Recent Posts

Monday, 4 July 2016

Member Level Modifiers


public members
     If we declare a member as public then we can access that member from anywhere but corresponding class should be visible (public) i.e., before checking member visibility we have to check class visibility.


default members
     If a member declared as the default, then we can access that member only with in the current package and we cannot access from outside of the package. Hence, default access is also known as package level access.

private members
* If a member declared as private then we can access that member only with in the current class.

* abstract methods should be visible in child classes to provide implementation where as private methods are not visible in child classes. Hence private-abstract combination is illegal for methods.

protected members
     If a member declared as protected then we can access that member with in the current package any where but outside package only in child classes.
protected = <default> + Kids of an another package (Only child reference)

* With in the current package we can access protected members either by parent reference or by child reference.

* But from outside package we can access protected members only by using child reference. If we are trying to use parent reference then we will get compile time error

final variables
* In general for instance and static variables it is not required to perform initialization explicitly JVM will always provide default values.

* But for the local variables JVM won't to provide ant default values compulsary we should provide initialization before using that variable.

final instance variables
* If the value if a variable is varied from object to object such type of variables are called instance variables.

* For every object a separate copy of instance variables will be created.

* For the normal instance variables it is not required to perform initialization explicitly. JVM will provide default values.

* If the instance variable declared as the final then compulsory we should perform initialization whether we are using or not otherwise we will get compile time error.

Rule
     For the final instance variables, we should perform initialization before constructor compilation i.e., the following are various places for this,
1. At the time of declaration
E.g
class Test {
   final int x = 10;
}
2. Inside instance block
E.g
class Test {
   final int x; {
      x = 10;
   }
}
3. Inside Constructor
E.g
class Test {
   final int x;
   Test() {
      x = 10;
   }
}
* Other tahn these if we are perform initialization any where else we will get compile time error.
class Test {
   final int x;
   public void m1() {
      x = 10; // C.E: Cannot assign a value to final variable x
   }
}

final static variables
* For the normal static variables it is not required to perform initialization explicitly, JVM will always provide default values.

* But for final static variables we should perform initialization explicitly otherwise we will get compile time error.
E.g
class Test {
   static int x; // Fine
}

class Test {
   final static int x; // C.E
}
Rule
* For the final static variables we should perform initialization before class loading compilation i.e., following are various places to perform this.
1. At the time of declaration
E.g
class Test {
   final static int x = 10;
}
2. Inside static block
E.g
class Test {
   final static int x; 
   static {
      x = 10;
   }
}
     If we are performing initialization any where else we will get compiletime error
class Test {
   final static int x;
   public void m1() {
      x = 10; // C.E: Cannot assign a value to final variable x
   }
}
final local variables
* For the local variables JVM won't provide any default values compulsory we should perform initialization before using that variable.
class Test {
   public static void main(String args[]) {
      int x;
      System.out.println("Hai"); // Fine
   }
} 

class Test {
   public static void main(String args[]) {
      int x;
      System.out.println(x); // C.E: variable x might not have been initialised.
   }
} 
* Eventhough local variable declared as the final it is not required to perform initialisation if we are not using that variable.
E.g
class Test {
   public static void main(String args[]) {
      final int x;
      System.out.println("Hai"); // Fine
   }
} 
* The only applicable modifier for local variables is final. If we are using any other modifier we will get compile time error

* Formal parameters of a method simply access as local variables of that method. Hence a formal parameter can be declared as final.

* If we declare a formal parameter as final within the method we can't change its value otherwise we will get compile time error.

static modifier
* static is the modifier applicable for variables and methods but not for classes (but inner classes can be declared as static)

* If the value of a variable is varied from object to object then we should go for instance variable. In the case of instance variable for every object a separate copy will be created.

* If the value of a variable is same for all objects then we should go for static variables. In the case of static variable only one copy will be created at class level and share that copy for every object of that class.
class Test {
   int x = 10;
   static int y = 20;
   public static void main(String[] args) {
      Test t1 = new Test();
      t1.x = 888;
      t1.y = 999;
      Test t2 = new Test();
       System.out.println(t2.x + " " + t2.y); // 10  999
   }
}
* static members can be accessed from both instance and static areas where as instance members can be accessed only from instance area directly i.e., from static area we can't access instance members directly otherwise we will get compile time error.

* For static methods compulsory implementation should be available where as for abstract methods implementation should not be available hence abstract static combination is illegal for methods.

* For static methods overriding concept is applicable hence with in he same class we can declare 2 main methods with different arguments.
class Test {
   public static void main(String[] args) {
      System.out.println("String[]");
  }
  public static void main(int[] args) {
      System.out.println("int[]");
  }
}

Output
String[]
* But JVM always call String argument main method only. The other main method we have to call explicitly just like a normal method call.

* Inheritance concept is applicable for static methods including main() method hence while executing child class if the child class doesn't contain main method then the parent class main method will be executed.

* It seems that overriding concept is applicable for static methods but it is not overriding. It is method hiding.

* Inside method implementation if we are using at least one instance variable then that method talks about a particular object, hence we should declare method as instance method.

* Inside method implementation if we are not using any instance variable then that method no way related to particular object, hence we should declare method such type of method as static method irrespective of whether we are using static variables or not.

synchronized modifier
* synchronized is the modifier applicable for methods and blocks. We cannot declare class and variable with this keyword.

* If a method or block declared as syncronized then at a time only one thread is allowed to operate on the given object.

* The main advantage of synchronized keyword is we can resolve data inconsistency problems. But the main dis advantage of synchronized keyword is it increases waiting time of thread and effects performance of the system. Hence, if there is no specific requirement it is never recommended to use synchronized keyword.

native modifier
* native is the modifier applicable for methods but not for variables and classes

* The native methods are implemented in some other languages like c and c++ hence native methods also known as foreign methods.

* The main objectives of native keyword are
1. To improve performance of the system.
2. To use already existing legacy non java code.

* For native methods implementation is already available in other languages and we are not responsible to provide implementation. Hence native method declaration should compulsory ends with ";"

* For native methods implementation should be available in some other languages where as for abstract methods implementation should not be available hence abstract native combination is illegal for methods

* native methods cannot be declared with strictfp modifier because there is no guarantee that old language follows IEEE754 standard. Hence native strictfp is illegal for methods.

* The main disadvantage of native keyword is it breaks platform independent nature of java because we are depending on result of platform dependent languages.

transient modifier
* transient is the modifier applicable only for variables and we cannot apply for methods and classes.

* At the time of serialization, if we don't want to save the value of a perticular variable to meet security, then we should go for transient keyword.

* At the time of serialization JVM ignores the original value of transient variable and default value will be serialization.

volatile modifier
* volatile is the modifier applicable only for variables but not for methods and classes

* If the value of a variable keep on changing such type variables we have to declare with volatile modifier.

* If a variable declared as volatile then for every thread a separate local copy will be created.

* Every intermediate modification performed by that thread will takes place in local copy instead of master copy.

* Once the value got finalised just before terminating the thread the master copy value will be updated with local stable value.

* The main advantage of volatile keyword is we can resolve data inconsistency problems.

* But the main disadvantage of volatile keyword is creating and maintaining a separate copy for every thread increases complexity of the programming and effects performance of the system. Hence, if there is no specific requirement it is never recommend to use volatile keyword and it is almost outdated keyword.

* volatile variable means it's value keep on changes where as final variable means its value never changes. Hence final volatile combination is illegal combination for variables.

Conclusion
* The only applicable modifier for local variables is final

* The modifiers which are applicable only for variables but not for classes and methods are volatile and transient.

* The modifiers which are applicable only for methods but not for classes and variables are native and synchronized.

* The modifiers which are applicable for top level classes, methods and variables are public, <default>, final.

* The modifiers which are applicable for inner classes but not for outer classes are private, protected, static.

Next Tutorial  Interfaces Tutorial

Previous Tutorial Class Modifiers

1 comment:

  1. Example 2 for final local variable: we will get compile time error please change output

    ReplyDelete