Java Modifiers, default/public/protected/private/ final/static/transient/synchronized/volatile

reference: http://www.studytonight.com/java/modifier-in-java.php

Modifiers are keywords that are added to change meaning of a definition. In java, modfiers are cateogrized into two types:

1. Access control modifier

2. Non Access modifier

1) Access control modifier

Java language has four access modifier to control access levels for classes, variable methods and constructor.

  • Default : Default has scope only inside the same package
  • Public : Public scope is visible everywhere
  • Protected : Protected has scope within the package and all sub classes
  • Private : Private has scope only within the classes

2) Non-access Modifier

Non-access modifiers do not change the accessibility of variables and methods, but they do provide them special properties. Non-access modifiers are of 5 types,

  1. Final
  2. Static
  3. Transient
  4. Synchronized
  5. Volatile

final: 

final field: its content cannot be changed, and it must be initialized when it is declared.

final class: cannot be inherited. String class in java.lang packages is a example of final class

final method: method declared as final can be inherited but you cannot override it.

static:

static modifiers are used to create class variable and class methods which can be accessed without instance of a class

static variable: static variable are class member that can be accessed without instance of the class. Static varibale has only one 

        single storage. static variable are initialized only once. static variable represent common proiperty of a class. 

class Employee
{
int e_id;
String name;
static String company_name = "StudyTonight";
}

static method: static method do not need instance of the class to access. main() method is the most common example. main() method

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

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

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

static block: static block is used to initialize static data member, static block executes before main() method

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();
     
    }
}

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

This is because non-static variables are related with instance of class 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 dont have any existence until an insatnce is created and associated with it

Transient modifier

when an instance variable is declared as transient, then its value does not persist when an object is serialized

Synchronized modifier

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

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.