Non access modifiers



Java provides a set of non access modifiers to produce some other functionality.
  1. static
  2. final
  3. abstract
  4. synchronized
  5. native
  6. transient
  7. volatile
  8. strictfp

Static


Static variables
  • We have learned about this thing as class variables in Java variables post in this blog.
  • You can get the idea about static variables after referring that post.

Static methods
  •  These methods are defined using 'static' keyword.
  • Class variables and also class methods ( static methods ) can be accessed using class name.
          Ex:-     className.staticMethodName( );
  • Static methods can directly access to other static members in the class.
  • But non static members can't access to static methods.
Try this program

public class StaticDemo{

   int x;
   static int y=10;

   public static void main(String args[]){
      Apple.test(); // Static methods are called using class name 
   }

   public static void test(){
      System.out.println(y);//Static variables can be used directly in static method
      //System.out.println(x); you can't access x in a static method
   } 
}



You can remove comment on last line and compile it.
Then you can see a error message as follow .




Static initialization block
  • This is a simple block With static keyword and { }.
  • Only static members can accessed to this block.
  • When you run the program, static initialization blocks are called to the order to their positions in the program
Try this program to understand static block.

public class StaticDemo{

   static{
      System.out.println("Static block 1");
   }

   public static void main(String args[]){ 
      System.out.println("Main method");
   }

   static{
      System.out.println("Static block 2");
   }

   static{
      System.out.println("Static block 3");
   }
}


Final




  • You can get the idea about this keyword thinking on its real meaning.
  • This can be used to classes, methods, objects and variables. 
  • If you used this once the value  can't be changed again.

Final variables


  • We have discussed three types of variables ( instance, static,local ) in this series. 
  • This ' Final ' keyword can be used all of these types.
  • These are like constants. 

Try this program.


public class FinalDemo{
   public static void main(String args[]){
       final int MAX_DIS = 5;
       System.out.println(MAX_DIS);

       //MAX_DIS = 10;
       //System.out.println(MAX_DIS); 
    }
}



Now you can see the output is 5.
  • You can see I commented some lines.
  • If you can remove these comments and compile it again. 
  • Then it will give a error message as follow. 

It means that the final variable can't be changed.

Final methods


  • Final methods cannot be overridden in other sub classes.
  • It means the behavior of that method cannot be changed.
Try this program


public class Fruit{
   public static void main(String args[]){      
      Fruit obj1 = new Fruit();
      Banana obj2 = new Banana();

      obj1.eat();
      obj2.drink();   
   }

   final void eat (){
      System.out.println("Fruit-->eat");
   }
}

class Banana extends Fruit{ 
   void drink(){
      System.out.println("Banana-->drink");
   }
   //void eat(){ }
}


  • Now I think you can understand what this program does. 
  • In my super class ( Apple ) I have defined two methods ( main, eat ).
  • The eat( ) method is final method.
  • Then I have created another sub class Banana which extends keyword. It means that Banana class is inherited by Apple class ( Apple - parent, Banana - child ). This is oop concept, so it will discussed later on the series.
  • When you compile and run this program it is ok and it gives the output.
  • Now you can remove comments in the program and compile it. Then you will be able to see the error message as follow.


Final classes


  • Final classes cannot be extended.
  • It is a final segment of class hierarchy of the program.
Try this program...

public class Apple{
   public static void main(String args[]){

      Apple obj1 = new Apple();
      Banana obj2 = new Banana();

      obj1.eat();
      obj2.drink();
   }

   final void eat (){
      System.out.println("Apple-->eat");
   }
}

final class Banana{ 
   void drink(){
      System.out.println("Banana-->drink");
   }
}

/* class Pear extends Banana{
 *    void steam() { }
 * }
 */



  • This is same as above example.
  • If you remove comment on this program and compile it, you will be able to see a error message.


Abstract




  • Abstract keyword can be applied to classes and instance methods only.
  • This means things incomplete or to be complete later.
  • Abstract is like opposite of 'final' keyword.

Abstract classes


  • The class can be declared as abstract class using ' abstract ' keyword.
  • But even if you not declared class as abstract, the can become abstract if there is at least one abstract method.
  • Abstract class can have non abstract methods also. 
  • An abstract class can never be instantiated. It can't be created objects using abstract class.

abstract class Apple{
   public abstract void mix(); //abstract class
   public void blend () {  };  //non abstract class
}




Abstract methods
  • Abstract methods doesn't have a method body. It has only specification.The method body is provided by sub class.
  • These methods can never be final and it can't be used private access modifier.
  • The method declaration of this methods are different from other methods.
Ex:-    public abstract void eat();

Try this.



abstract class Apple{
   public abstract void eat();
   public void cook(){ } ;
}

class Mango extends Apple{
   public void eat(){
      System.out.println("Mango-->eat()");
   }
}

public class Fruit{
   public static void main(String args[ ]){

      Mango obj1 = new Mango();
      Apple obj2 = new Mango();
      //Apple obj3 = new Apple(); 
      
      obj1.eat();
      obj2.eat();
   }
}


See more about abstraction.

Synchronized


  • This can only be used to methods or you can use is as a block.
  • If you use this keyword in a method, that indicate the method can be accessed by only one thread at a time.
  • You can use any of access modifiers to set access level.
Ex:-    public synchronized void DisplayDate( ) {   }


You may look at this post which is about Synchronization.



Native

  • This can only be used for methods.
  • Native methods are used to access some other platforms by loading native libraries. 
  • Native methods do not have method body.
Ex:-    native void DoSomething( );


Transient

  • This can be used to variables.
  • The types of variables are used to signify the JVM to skip this variable in serialization process.
  • In serialization process, it can be stored the state of object.
  • Serialization transform object into output type.

public transient int avg = 10; //transient value
public int spd;  //persist value

Volatile


strictfp

  • This can be applied to interfaces, classes and methods.
  • Strictfp ensures that you get exactly the same results from your floating point calculations on every platform.
  • But most programmers never need to use strictfp modifier, because it affects only a very small class of problems in programming.
  • A strictfp method ensures that all code in the method is executed strictly.


    Non access modifiers Non access modifiers Reviewed by Ravi Yasas on 5:22 AM Rating: 5

    No comments:

    Powered by Blogger.