Inheritance, in-spite of it being an important/powerful feature of object oriented programming, there would be some instances where you would like to stop a class from being inherited. With that, here is the first use of the keyword "final". A class that has a "final" keyword in front of it cannot be extended, in other words final stops inheritance of a class. The other power full concept of object oriented programming "overriding", can also be stopped by using the keyword "final". A method cannot be overridden if it has the keyword "final" in-front of it.
As shown in the following figure, we provide two use-cases of "final".
There is another use-case of the the key word final, and that is to "declare constant values". If you are familiar with C programming language, it is similar to the C keyword "constant". Thus a variable with the "final" keyword in-front of it cannot be assigned any other values. So, a final variable will have to be initialized.
public class finalExample { public static void main(String[] args) { final int finalVar = 100; System.out.println(" Final variable value " + finalVar); // This is not allowed, this throws a compilation error. //finalVar = 200; } }
The keyword "static" in Java is used in many scenarios, let us take a look at them one after the other. "static" key word can be used with a variables, methods, certain blocks of code with in a class. A variable declared static is instantiated only once and hence it is shared across all the object. A method that declared static has few restrictions, first, they can only call static variables inside its body. Second, they can only call other static methods and third, they cannot use the keywords super or this.
Let us look at an example and understand the usage of it.
class staticClass { /* This counter is incremented every time, any of * the methods in this class is called. */ static int counter = 0; static int sVar1; static int sVar2; int var1; static void method1() { int result; /* This below statement throws an error since, as var1 * is not static variable. As we know, static methods can * only call static variables. */ //result = sVar1 + sVar2 + var1; result = sVar1 + sVar2; System.out.println(" Inside static method: " + result); counter++; } void method2() { counter++; System.out.println(" This method is declared just for the heck of it"); } } public class staticExample { public static void main(String[] args) { staticClass obj1 = new staticClass(); staticClass obj2 = new staticClass(); System.out.println(" ...................................."); System.out.println(" Calling non static method, 'method2'"); System.out.println(" ...................................."); obj1.method2(); System.out.println("\n ......................"); System.out.println(" Calling static methods"); System.out.println(" ......................"); // Static members can be called directly with out instantiating any objects. staticClass.sVar1 = 100; staticClass.sVar2 = 200; staticClass.method1(); staticClass.method1(); obj2.method2(); System.out.println(" Number of time, (combined count) of all the methods calls" + " in the staticClass: " + staticClass.counter); } }
As you noticed in the above example, an object is not needed to invoke the static members of a class. All the static members can be directly invoked with the help of its class name, eg..staticClass.method1();.
Now that we know the usage/meaning of the key word static, let us try to answer this question. why is that a main method in Java is always declared as "static". That is because JVM need not instantiate an object of the class that has main method, it can directly call the main method using the class name.
Compile/Run this program and here is the output:
.................................... Calling non static method, 'method2' .................................... This method is declared just for the heck of it ...................... Calling static methods ...................... Inside static method: 300 Inside static method: 300 This method is declared just for the heck of it Number of time, (combined count) of all the methods calls in the staticClass: 4