Modifiers in Java
Modifiers are keywords that are added to change the meaning of a definition. In Java, modifiers are categorized into two types,
- Access control modifier
- 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 subclasses
- 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 with special properties. Non-access modifiers are of 5 types,
- Final
- Static
- Transient
- Synchronized
- Volatile
Final
Final modifier is used to declare a field as final i.e. it prevents its content from being modified. A final field must be initialized when it is declared
Example :
class Cloth { final int MAX_PRICE = 999; //final variable final int MIN_PRICE = 699; final void display() //final method { System.out.println("Maxprice is" + MAX_PRICE ); System.out.println("Minprice is" + MIN_PRICE); } }
A class can also be declared as final. A class declared as final cannot be inherited. String class in java.lang package is an example of a final class. A method declared as final can be inherited but you cannot override(redefine) it.
Static Modifier
Static Modifiers are used to create class variable and class methods which can be accessed without an instance of a class. Let's study how it works with variables and member functions.
Static with Variables
Static variables are defined as a class member that can be accessed without any object of that class. A static variable has only one single storage. All the object of the class having static variable will have the same instance of a static variable. Static variables are initialized only once.
The static variable is used to represent a common property of a class. It saves memory. Suppose there are 100 employees in a company. All employee has its unique name and employee id but company name will be same all 100 employees. Here company name is the common property. So if you create a class to store employee detail, company_name field will be marked as static.
Example
class Employee { int e_id; String name; static String company_name = "StudyTonight"; }
Example of static variable
class ST_Employee { int eid; String name; static String company_name ="StudyTonight"; 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(); ST_Employee se2 = new ST_Employee(); se2.eid = 108; se2.name = "ankit"; se2.show(); } }
Output :
104 Abhijit StudyTonight 108 ankit StudyTonight
Static variable vs Instance Variable
Static variable | Instance Variable |
---|---|
Represent common property | Represent unique property |
Accessed using class name | Accessed using object |
get memory only once | get new memory each time a new object is created |
Example
public class Test { static int x = 100; int y = 100; public void increment() { x++; y++; } public static void main( String[] args ) { Test t1 = new Test(); Test t2 = new Test(); t1.increment(); t2.increment(); System.out.println(t2.y); System.out.println(Test.x); //accessed without any instance of class. } }
Output :
101 102
See the difference in the value of two variable. Static variable y shows the changes made to it by increment() method on the different object. While instance variable x show only the change made to it by increment() method on that particular instance.
Comments
Post a Comment