Previous | Home | Next |
In java there are some modifiers that are not access modifiers, still they can modify the way a class or a class member is to be used, we call such modifiers the usage modifiers. Following are usage modifiers used in java :
-
The final Modifier : The final modifier can be applied to a class, a method or a variable. In general if the an element declared final, is a variable, it means that the value of the variable is constant, and cannot be changed. If a class is declared final, it means the class cannot be inherited, and also the final method cannot be overridden. It can be declared as,
- final int i = 5;
- final double d = 2.5;
-
The static Modifier : The static modifier can be applied to variables, methods, and a block of code inside a method. The static elements of a class are visible to all the instances of that class. Thus, if one instance of the class makes a change to a static element, all the instances will see that change.
A static variable belongs to the class, not to a specific instance of the class, therefore, is initialized when the class is loaded. A static variable may be referenced by an instance of the class in which it is declared or by the class name itself. For better understanding, consider the following code snippet,
The above declaration tell us that the values of i and d will remain constant throughout the course of the program and cannot be changed. For understanding the final modifier in terms of classes and methods, consider the following code snippet,
Example
Java Tutorialslass ShowFinal { private final int i=10; void Java Tutorialsalculate(int i) { System.out.println("The value of i is:"+i); } } Java Tutorialslass FinalExample { public static void main(String args[]) { final ShowFinal obj=new ShowFinal(); //obj.i=7; //compilation error as value of i is final can't be changed obj.Calculate(10); } }
Example
Java Tutorialslass StaticModifier { static int static_counter=0; int Java Tutorialsounter =0; public StaticModifier() { static_counter++; System.out.println("The value of static counter is :"+static_counter); Java Tutorialsounter++; System.out.println("The value of normal counter is :"+counter); } } Java Tutorialslass StaticModifierExample { public static void main(String args[]) { StaticModifier obj=new StaticModifier(); // static_counter value=1, counter =1 StaticModifier obj1=new StaticModifier(); // static_counter value=2, counter =1 //Since the modifier of static_counter is static, which is available to all the instances of the class, // the change in value of static_counter in any of the instance will be reflected in other instances. // while the counter value will remain constant whenever any instance is created as it is not declared static. } }
Just like a static variable, a static method also belongs to the class in which it is defined and not to a specific instance of the class. Therefore, a static method can only access the static members of the class.
In other words, a method declared static in a class cannot access the non-static variables and methods of the class. Because a static method does not belong to a particular instance of the class in which it is defined, it can be called even before a single instance of the class exists.
For example, every Java application has a method main(), which is the entry point for the application execution. It is executed without instantiating the class in which it exists. This can be explained by the following example,
Example
Java Tutorialslass StaticModifierExample2 { static int a=10; static int b=20; static void sum() { System.out.println("The value of sum of two numbers is :"+(a+b)); } public static void main(String args[]) { sum(); // static method is called before instantiating any object of StaticModifierExample2 } }
Previous | Home | Next |