Skip to main content
Active reading. [<http://english.stackexchange.com/questions/4645/is-it-ever-correct-to-have-a-space-before-a-question-or-exclamation-mark#comment206109_4645>] - but more could definitely be done, e.g. near "access" (several places).
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

Access Modifiersmodifiers are there to restrict access at several levellevels.

Public  : itIt is basically as simple as you can access from any class eitherwhether that is in same package or notnot.

To access if you are in same package you can access directly, but if you are in otheranother package then you can create an object of the class.

Default  : itIt is accessible in the same package from any of the class of package.

toTo access you can create an object of the class. butBut you can not access this variable outside of the package.

Protected  : you can access variables in same package as well as subclass in any other package. so basically it is default + Inherited behavior.

To access protected field defined in base class you can create object of child class.

Private  : it can be access in same class.

In non-static methods you can access directly because of this reference (also in constructors)but to access in static methods you need to create object of the class.

Access Modifiers are there to restrict access at several level.

Public  : it is basically as simple as you can access from any class either that is in same package or not.

To access if you are in same package you can access directly but if you are in other package then you can create object of class.

Default  : it is accessible in same package from any of the class of package.

to access you can create object of class. but you can not access this variable outside of the package.

Protected  : you can access variables in same package as well as subclass in any other package. so basically it is default + Inherited behavior.

To access protected field defined in base class you can create object of child class.

Private  : it can be access in same class.

In non-static methods you can access directly because of this reference (also in constructors)but to access in static methods you need to create object of the class.

Access modifiers are there to restrict access at several levels.

Public: It is basically as simple as you can access from any class whether that is in same package or not.

To access if you are in same package you can access directly, but if you are in another package then you can create an object of the class.

Default: It is accessible in the same package from any of the class of package.

To access you can create an object of the class. But you can not access this variable outside of the package.

Protected: you can access variables in same package as well as subclass in any other package. so basically it is default + Inherited behavior.

To access protected field defined in base class you can create object of child class.

Private: it can be access in same class.

In non-static methods you can access directly because of this reference (also in constructors)but to access in static methods you need to create object of the class.

Source Link
Prashant
  • 2.6k
  • 2
  • 23
  • 26

Access Modifiers are there to restrict access at several level.

Public : it is basically as simple as you can access from any class either that is in same package or not.

To access if you are in same package you can access directly but if you are in other package then you can create object of class.

Default : it is accessible in same package from any of the class of package.

to access you can create object of class. but you can not access this variable outside of the package.

Protected : you can access variables in same package as well as subclass in any other package. so basically it is default + Inherited behavior.

To access protected field defined in base class you can create object of child class.

Private : it can be access in same class.

In non-static methods you can access directly because of this reference (also in constructors)but to access in static methods you need to create object of the class.