Saturday, 22 June 2013

What are different modifiers in JAVA

There are 2 types of modifiers in JAVA.

1. Access Modifiers in JAVA

  • private     - visible to class
  • public      - visible to world
  • protected - visible to package and subclasses
  • default     - visible to package 
Classes can be either default or Public.

Default class can be accessed by other classes in same package.
Public class can be accessed by classes from any other classes (in same or different package)

Class members can have any of the modifier.

Class members like variables and methods can be private, public, protected or default.
Private members can be accessed only within the class.
Public members can be accessed from outside the class.
Default members and protected members are same except one difference that default members have only package visibility. While Protected members have package visibility as well as in other sub classes.


2. Non-Access Modifiers in JAVA
  • static
  • final
  • abstract
  • synchronized and volatile - used in threads

Classes can be - final and abstract

Methods can be static final abstract

Variables can be static, final, synchronized and volatile.










No comments:

Post a Comment