Difference between Final & Static in Java

In Java,we use these keywords in our development all the time and somehow never realize in which situation we get a compile error until the intellisense prompts us or when we are asked question in interview about it.So let’s see both the keywords in detail.

Final keyword can be applied to a
1.A class
2.A method
3.A variable
Let’s see in each scenario how the keyword behaves.

1.Class

When a final keyword is applied to a class it cannot be inherited.

final class A{
public void methodA(){
System.out.println("A");
}
}
class B extends A{                          //COMPILE ERROR 
}
This will give a compile error.

2.Method

When final is used in a method, that method cannot be overrided or redeclared.
So what do I mean by redeclared?
Let’s see with an example.

class A{
public final void doSomethingA(){
System.out.println("A");
}
class B extends A{
public void doSomethingA(){  //COMPILE ERROR 
} 

doSomethingA in class B will give a compile error since method marked as final in class A cannot be overrrided.

class A{
public final void doSomethingA(){
System.out.println("A");
}
class C{
public void metdoSomethingA(){  //COMPILE ERROR 
}  

doSomethingA in class C will also give a compile error even though class C DOES NOT extend class A since method marked as final in class A cannot be redeclared.
Meaning the method named doSomethingA() in class cannot be used in any other class even though it doesnt extend it.Some other method name has to be used.

3.Variable

Once a variable is initialized using final,it’s value has to be initialized by the programmer.JVM does not initialize it for us
Once initialized that variable cannot be reassigned a value.

class A{
final int x;      //COMPILE ERROR x should be initialized.
final int x1=10;
}
class B extends A{
void m1(){
x=12;                        //COMPILE ERROR x cannot be changed.
}
}

Also when a variable is declared as final,hiding of local variable does not work.


Now let’s come to static keyword and apply to each case of Class ,method and variable like before.

1.Class

A class cannot be marked as static except in case if Inner class.

2.Method

A method marked as static cannot be overrided .However,subclass can recreate this method by marking it as static.

A method marked as static cannot be overrided .However,subclass can recreate this method by marking it as static.

class A{
static void m1(){
System.out.println("A");
}}
class B extends A{
static void m1(){
System.out.println("B");
}}
class App{
public static void main(String[]args){
A a=new B();
a.m1();                    //OUTPUT:A
B b=new B();
b.m1();                   //OUTPUT:B
}}
OUTPUT:
A
B

3.Variable

If a variable is marked as static, it is saved in the static memory of the class.
static variable can be access in 3 ways:
1.Using Object
2.Using Classname
3.Directly
In case of Direct access ,there are 2 ways accessing a static variable or entities(methods & variables) in the same class or outside that particular class.

class A{
static int x=10;

public static void main(String[]args){
A a=new A();
System.out.println(a.x);                        //USING OBJECT
System.out.println(A.x);                        //USING CLASSNAME
System.out.println(x);                        //DIRECTLY SAME CLASS
}}
For accessing static entities OUTSIDE THE CLASS.We have import them statically for DIRECT ACCESS.
package com.Entities;
import static com.asserts.A.x;                    //IMP
import static com.asserts.A.m1;
import static com.asserts.A.*;
class A{
static int x;
static void m1(){
System.out.println("A");
}}


class B extends A{
static int x=10;
}
class App{
public static void main(String[]args){
System.out.println(new A(x));
System.out.println(A.x);
System.out.println(x);          //import needed for direct access
m1(3);
}
}

Static and non static instance variables restrictions.

Sometimes while using static variables we sometimes face an error stating non static variable cannot be referenced from static context. This is because there are some restrictions while using static and non static variables in certain methods.
Lets see some static and non static instance variables restrictions.

class A{
int x;
static int y;
static void m1(){
y=10;                  //WORKS FINE
x=10;                  //COMPILE ERROR SAYING non static variable cannot be referenced from static context.

}
 void m2(){
x=10;
y=10;
x=y;
}
//ALL WORK FINE

public static void main(String[]args){
x=10;                                  //COMPILE ERROR 
//non-static variable x cannot be referenced from a static context

}}

So that’s all for final and static in java.If any mistake please feel free to correct me in the comments(Grammatically or otherwise).

Happy Learning 🙂

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *