reference:http://www.studytonight.com/java/modifier-in-java.php
Modifiers is keywords that is added to the change meaning of a definition. In Java, Modfiers is cateogrized into and types:
1. Access Control modifier
2. Non Access modifier
1) Access control modifier
Java language have four access modifier to control access levels for classes, variable methods and constructor.
- Default: Default have scope only inside the same package
- Public : Public scope is visible everywhere
- Protected: Protected have scope within the package and all sub classes
- Private: Private have scope only within the classes
2) non-access Modifier
Non-access modifiers don't change the accessibility of variables and methods, but they do provide them special properties . Non-access modifiers is of 5 types,
- Final
- Static
- Transient
- Synchronized
- Volatile
Final
Final field:its content cannot be changed, and it must was initialized when it was 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 and you cannot override it.
Static
Static modifiers is used to create class variable and class methods which can be accessed without instance of a class
Static variable:static variable is class member that can be accessed without instance of the class. Static Varibale have only one
Single storage. Static variable is initialized only once. Static variable represent common proiperty of a class.
Class Employee{int e_id; String Name;static string company_name = "Studytonight";}
The static Method:static method does not need instance of the class to access. Main () method is the most common example. Main () method
is declared as static because it's 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[] A RG) { 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 is referenced from a static context?
This is because non-static variables was related with instance of class and they get created when instance of a class is C Reated by using new
operator. So if you try to access a non-static variable without any instance compiler would complain because those variables is not Yet created and they dont has any existence until an insatnce are created and associated with it
Transient modifier
When a instance variable is declared as transient and then it value does not persist when a object is serialized
Synchronized modifier
When a method was synchronized it can be accessed by only one thread at a time.
Volatile modifier
Volatile modifier tells the compiler that the volatile variable can is changed unexpectedly by other parts of your program .
Volatile variables is used in case of the multithreading program.
Java Modifiers, Default/public/protected/private/final/static/transient/synchronized/volatile