Thursday 2 June 2016

Modifiers in Java Language

The Java modifiers divided into two categories.

1-Access Modifiers
2-Non Access Modifiers

The below example shows the uses of modifiers 

public class Employeee {
  
static final double length = 9.5;
protected static final int weight = 42;
public static void main(String[] arguments) {
   // body of method
}

1-Access Modifiers:
Java provides a number of access modifiers to set the access levels of classes,variables,methods and constructors.The four access levels  are:public,protected,default,private

When the visibility limited to the package level,the default modifier is used.
When the visibility limited to the class level,the private modifier is used.
When the visibility limited to the sub class level,the protected modifier is used.
When the visibility level accessible by world,the public modifier is used.

















2-Non Access Modifiers: Non-access modifiers do not change the accessibility of variables and methods, but they do provide them special properties.The example of Non-access modifiers are 
Final,Static,Transient,Synchronized and Volatile.

1-Final
When final modifier is used to declare a field as final i.e.it prevents the content from being modified.Final field must be initialized when it is declared.
A class declared as final cannot be inherited.String class in java.lang package is a example of final class and the method declared as final can be inherited but you cannot override.

Demo Example :

class Person
{
 final int MAX_Height = 160;         //final variable
 final int MIN_Weight = 60;
 final void display()            //final method
 {
  System.out.println("Max height is" + MAX_Height );
  System.out.println("Min weight is" + MIN_Weight);
 }
}

2-static
static modifier are used with class ,method and variables.

static modifier with variables

Static variables are defined as a class member that can be accessed without any object of that class. Static variable has only one single storage. All the object of 

the class having static variable will have the same instance of static variable. Static variables are initialized only once.


Static Method

Static methods do not need instance of its class for being accessed. main() method is the most common example of static method. main() method is declared as static 

because it is called before any object of the class is created.

Example :

class Test 
{
 public static void cube(int x) 
 {
  System.out.println(x*x*x);
 }

 public static void main (String[] arg) 
 {
   
  cube(2)   //static method cube() is called without any instance of class.
 }
}


Static block

Static block is used to initialize static data member. Static block executes before main() method.

Example

class ST_Employee
{
   int eid;
   String name;
   static String company_name;
    
   static {
    company_name ="StudyTonight";    //static block invoked before main() method  
    }

    public void show()
    {
        System.out.println(eid+" "+name+" "+company_name);
    }
    public static void main( String[] args )
    {
     ST_Employee se1 = new ST_Employee();
     se1.eid = 104;
     se1.name = "Abhijit";
     se1.show();
     
    }

}

Very important question  ??

Why a non-static variable cannot be referenced from a static context ?

This is because non-static variables are related with instance of class(object) and they get created when instance of a class is created by using new operator.So if 

you try to access a non-static variable without any instance compiler will complain because those variables are not yet created and they don't have any existence until 

an instance is created and associated with it.Example of accessing non-static variable from a static context

class ABC
{
 int a;
 public static void main(String[] args)
 {
  a=10;
 }
}


o/p 
compiler error: non-static variable count cannot be referenced from a static context
Same example using instance of class

class ABC
{
 int x;
 public static void main(String[] args)
 {
  ABC t1=new ABC();
  t1.x=20;  //works fine with instance of class
 }
}

2- Why main() method is static in java ?

Because static methods can be called without any instance of a class and main() is called before any instance of a class is created.

3-Transient modifier

When an instance variable is declared as transient, then its value doesn't persist when an object is serialized

4-Synchronized modifier

When a method is synchronized it can be accessed by only one thread at a time.

5-Volatile modifier

Volatile modifier tells the compiler that the volatile variable can be changed unexpectedly by other parts of your program.Volatile variables are used in case of  multithreading program.

No comments:

Post a Comment