TwitterFacebookGoogle PlusLinkedInRSS FeedEmail

Sunday, November 11, 2012

Modifiers in Java

Modifiers:
         Modifiers are keywords that are added to change meaning of a definitions. Modifiers in java are categorized into two type:
  • Access Control Modifiers.
  • Non Access Modifiers.
Access Control Modifiers: 
          Java Language has four access modifiers to control access levels for classes, variables, methods and constructors.These are
  • Default  :- Default has scope only inside Package. 
  • Public:- Public scope is visible everywhere.
  • Protected :- Protected has scope within the package and all subclasses.
  • Private:- Private has scope only within the classes.
Non Access Modifiers:
            Java language has wide variety of Non Access Modifiers. Such as static, final, volatile, transient, abstract, synchronized.

Static Modifier: 
           Static Modifier are used to create class methods and Class variables, which can be access without 
instance of the class.
Static Variables: Static member is define as a class member that can be accessed without any object of
that class. Normally a class member must be accessed with an object of its class. Static Variables are initialized only once. 
Static Methods: You can also declare a methods as static. Static methods do not use any instance variables of any object of the class they are defined in. The most common example of a static method is main( ). main( ) is declared as static because it must be called before any objects exist.
Example: 
 Final Modifier:
              Final Modifier are used to declare a field as final i.e prevent its content from being modified.
Final Variables: Final field must be initialize when it is declare. A reference variable declared final can never be reassigned to refer to an different object. However the data within the object can be changed. So the state of the object can be changed but not the reference.
Example:

Final Methods: Method declare as final cannot be overridden. Overridden method is described in detail in our next chapter. 
Example:

Final Class: When  class is declare as final it cannot be inherited. Therefore using Final keyword before a class name prevent it from being inherited.
Example: 
 Transient Modifier:
               When an instance variable is declare as transient, then its value need not persist when an object is stored i.e when an object of that class is created the contents of transient variable is not stored.
Example:

Abstract Modifier:
 Abstract Methods: Abstract methods are methods declare without any body within an abstract class. The methods body is given by the subclass. Abstract methods can never be final or static. Any class that extends an abstract class must implement all the abstract methods of the super class unless the subclass is also an abstract class.
Abstract Class: If a class contain any abstract method, then the class is declare as Abstract class. An abstract class can never be instantiated i.e you cannot create object of an abstract class.
Example:
Synchronized Modifier:

             When a method is synchronized then it can be accessed by only one thread at a time. Synchronized modifier can be applied with any of the four access level modifiers.
Example:     public synchronized void test(){}           

Volatile Modifier:

           Volatile Modifier tells the compiler that the volatile variable can be changed unexpectedly by other parts of your program. Volatile Modifier are used in case of multithreaded program. A thread accessing the volatile variable must always merge its own private copy of the variable with the master copy in the memory.
 Example:


                                       <--PREV | NEXT-->