The Best Technical Blog Ever

twitterrss

Java Non Access Modifiers

There are number of non-access modifiers available in java, such as


  • static
  • final
  • abstract
  • synchronized
  • transient
  • volatile
  • native

1. The static Modifier:

  • Static Variables:

    A variable that is declared with the static keyword in a class is called static variable. It cannot be local. But it is declared outside of a method, constructor or a block. Static variables are stored in static memory. All objects of the same class share a single copy of static variables. They are created when the program starts and destroyed when the program stops. Default values are same as instance variables. Static variables in a class are initialized before any object of that class can be created or any static method of the class runs. Static variables are usually used as constants. Static variables can be accessed by using ClassName.VariableName.


    Example:
    private static double salary;

  • Static Methods:

    A method that is declared with the static keyword in a class is called static method and is used to create methods that will exist independently of any instances created for the class. Static methods don't use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those parameters, with no reference to variables. Static variables can be accessed by using ClassName.methodName.


    Example:
    public static int add() 
    {
    	 //method body
    }

2. The final Modifier:

Final modifiers are applicable to Class, Method ,Instance Variable, Local Variable & also Method arguments.


  • Final Class :

    It cannot be extended by any other class. So no class can inherit any feature from the final class.


    Example:
    public final class A 
    {
        // body of class
    }
    

  • Final Method :

    It cannot be overridden by any subclass. It prevents a method from being modified in a subclass.


    Example:
    public class A{
        public final void setName(){
          	 // body of method
        }
    }

  • Final Variable :

    It can be explicitly initialized only once. It's value cannot be changed. Final variables are like constants.


    Example:
    public static final int age = 20;
    static final String name = "Solsoft";
    

3. The abstract Modifier: (opposite to final)

  • abstract Class:

    The abstract class is declared using ' abstract ' keyword. The purpose of abstract class is to be extended. If a class contains abstract methods then the class should be declared abstract. Otherwise compiler will throw error. An abstract class may contain both abstract methods as well normal methods. We can't create objects using abstract class.


    Example:
    abstract class Student    //abstract class
    {
        private String name;        
        public abstract void total_Mark();  //abstract method
        public void print();   //normal method 
    }
    

  • abstract Methods:

    Abstract methods doesn't have a method body. Abstract method is declared without any implementation. The method body is provided by sub class. These methods can never be final and it can't be used as private. 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. If a class contains abstract methods then the class must be declared as abstract. But an abstract class does not need to contain abstract methods. These methods end with semicolon.


    Example:
     public abstract void getName();

4. The synchronized Modifier:

When a method is synchronized, it can be accessed by only one thread at a time. These type of methods have synchronized keyword. It can be applied with any of the four access modifiers.


Example:
public synchronized void getName(){
  .......
} 

5. The transient Modifier:

The transient modifier tells the Java object serialization subsystem to exclude/skip the field when serializing an instance of the class. When the object is then deserialized, the field will be initialized to the default value; i.e. null for a reference type, and zero or false for a primitive type.


Example:
class Student {
    transient int rollno=23;  // will not persist
    public int mark;  // will persist
}
 

6. The volatile Modifier:

The volatile is used to inform the compiler that a thread accessing the variable must always merge its own private copy of the variable with the master copy in the memory. It also tells the compiler that the volatile variable can be changed unexpectedly by other parts of your program. Accessing a volatile variable synchronizes all the cached copied of the variables in the main memory. It can only be applied to instance variables, which are of type object or private. It used in multithreading program. The fields marked as volatile can be safely accessed and updated in a multi-thread application without using native or standard library-based synchronization.


Example:
class Student {
   volatile int a;  // volatile field declaration
}

7. Native Methods:

Native Methods are specified in the class as method prototypes with prefix native keyword, no method body is defined in the Java class. But that method is implemented on a platform dependent code. They are also called as Foreign methods.


Example:
 native void add();  // native method declaration
Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status