Difference Between Final and Static in Java

Difference Between Final and Static in Java

7 mins readComment
Esha
Esha Gupta
Associate Senior Executive
Updated on Mar 28, 2024 18:18 IST

Have you ever wondered about what are the differences between static and final keyword in Java? If not, you are at the right place as in this blog we have discussed how they are different in detail. Let's understand more!

The final keyword is all about creating unmodifiable variables, methods, or classes, ensuring immutability and preventing overriding or inheritance. In contrast, the static keyword is about memory management, creating class-level variables and methods that can be accessed without needing an instance of the class. In this blog, we will see the differences between them in detail!

Table of Content

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

– / –
6 weeks
β‚Ή15 K
3 months
β‚Ή2.05 K
3 months
– / –
170 hours
β‚Ή5.4 K
1 month
– / –
6 months
β‚Ή17 K
3 months
β‚Ή5.5 K
1 month
– / –
2 months

Difference Between Final and Static in Java

Below is a table of differences between final and static in Java.

Aspect

Final

Static

Primary Use

To create constants (final variables), prevent method overriding (final methods), and prevent inheritance of classes (final classes).

To create class-level variables (static variables) and methods (static methods) that are shared across all instances of the class.

Scope

It can be applied to variables, methods, and classes.

Mainly applied to variables and methods.

Initialization

Final variables need to be initialized at the time of declaration or inside the constructor.

Static variables are initialized when the class is first loaded by the JVM.

Access

Accessed through object references for variables; methods and classes are accessed in the standard way.

Accessed directly using the class name, without needing an instance of the class.

Inheritance and Overriding

Final methods cannot be overridden; final classes cannot be extended.

Static methods can be hidden by subclasses (not overridden); static variables are shared among subclasses.

Memory Allocation

Allocated when an instance of the object is created.

Allocated in a separate part of memory (method area or class area) and shared among all instances.

Lifetime

Tied to the object’s lifetime.

Exists from when the class is loaded until the program ends.

 

What is the Final Keyword in Java?

In Java, the final keyword is used to denote constants. It can be used in several contexts.

1. Final Variables: When final is applied to a variable, it becomes a constant. This means the value of this variable cannot be changed once it is initialized. A final variable must be initialized at the time of its declaration or in the constructor if it's an instance variable. For example:


 
final int MAX_VALUE = 100;
Copy code

Here, MAX_VALUE cannot be changed once it is set to 100.

2. Final Methods: When a method is declared as final, it cannot be overridden by subclasses. This is used to prevent any alteration in the method's implementation in the inheritance chain. For instance:


 
public final void display() {
System.out.println("This is a final method.");
}
Copy code

In this case, if the method display belongs to a superclass, then no subclass can override this method.

3. Final Classes: A class declared as final cannot be subclassed. This is particularly useful when creating an immutable class like the String class in Java. For example:


 
public final class MyFinalClass {
// Class contents
}
Copy code

Here, no other class can extend MyFinalClass

Points to be noted:

  • Final variables can be initialized only once, and their values cannot be modified.
  • Final methods cannot be overridden by subclasses.
  • Final classes cannot be extended by other classes.
  • The final keyword provides a degree of β€œsecurity” by protecting your code against alteration and misuse in certain contexts, especially in large and complex software projects.
  • It can also be useful in optimizing code, especially in terms of performance, as the compiler can make certain assumptions about final variables or methods.

What is Static Keyword in Java?

The static keyword in Java is used to indicate that a particular member (variable, method, or block) belongs to the class itself rather than to instances of the class.

1. Static Variables: When the static keyword is applied to a variable, it becomes a class variable. It is shared among all instances of the class, meaning there is only one copy of this variable in memory, regardless of how many instances of the class exist. A static variable must be declared in a class but outside a method or any block of code. For example:


 
class MyClass {
static int staticCounter; // Static variable
}
Copy code

Here, staticCounter is shared by all instances of MyClass. If one instance changes its value, the changed value is seen by all other instances.

2. Static Methods: Static methods, like static variables, belong to the class rather than any particular instance. They can be called without creating an instance of the class. Static methods can only access other static members (variables and methods) of the class. They cannot directly access instance variables or methods. For example:


 
class MyClass {
static void staticMethod() {
System.out.println("This is a static method.");
}
}
Copy code

staticMethod can be called as MyClass.staticMethod() and does not require an object of MyClass to be created.

3. Static Blocks: Static blocks are used for static initializations of a class. This block gets executed when the class is loaded in the memory by Java ClassLoader. It is used to initialize the static data members and is executed before the main method at the time of class loading. For example:


 
class MyClass {
static {
// static block
System.out.println("This block is executed when the class is loaded.");
}
}
Copy code

Points to be noted:

  • Static variables are shared among all instances of a class, with only one copy existing in memory.
  • Static methods can be called without an instance of the class and can only access other static members.
  • Static blocks are used for static initializations and are executed when the class is loaded.
  • The static keyword contributes to memory efficiency because it allows variables or methods to be shared among all instances of a class.
  • It also plays a role in organizing methods and variables that are utility-like and do not belong to any specific object instance.

What are Identifiers in Java?

Getting Started with Java Hello World Program

Understanding Variables in Java

Conclusion

Thus, the final and static keywords in Java serve distinct purposes and have different implications on the class design and behaviour. Understanding these keywords is essential for effective Java programming, particularly in designing robust, secure, and efficient classes.

FAQs

What does the final keyword signify in Java?

The final keyword in Java is used to apply restrictions on classes, methods, and variables. When applied to a variable, it makes the variable a constant, meaning its value cannot be modified once initialized. When used with a method, it prevents method overriding in subclasses. And when applied to a class, it prevents the class from being subclassed. In essence, final is used to declare entities that cannot be modified after they are set.

What does the static keyword signify in Java?

The static keyword in Java is used to indicate that a particular field, method, or block belongs to the class, rather than instances of the class. This means that static members can be accessed without creating an instance of the class. Static variables are shared among all instances of a class, making them class variables. Static methods can be invoked without an object, and static blocks are used for static initializations.

Can a final variable be static in Java?

Yes, a variable can be both final and static in Java. This combination is used to define constants that are common to all instances of a class. For example, public static final int MAX_SIZE = 100; declares a constant MAX_SIZE that is shared across all instances of the class and cannot be changed.

How do final and static affect inheritance in Java?

Final and static keywords affect inheritance differently. A final class cannot be extended, meaning no subclass can inherit from a final class. A final method cannot be overridden by subclasses, which means the functionality defined by the final method is the same for all subclasses. On the other hand, static members (methods or variables) are not inherited in the same way instance members are; instead, they belong to the class level. While static methods can be hidden by subclasses (if they declare a static method with the same signature), this is not the same as overriding and does not follow inheritance rules.

What are the best practices for using final and static in Java?

Best practices for using final:

  • Use final for variables that should not change after initialization.
  • Apply final to methods that should not be overridden to maintain the intended behavior across subclasses.
  • Use final on classes that are not meant to be extended, often for reasons of security, simplicity, or design.

Best practices for using static:

  • Use static variables for class-level constants or fields shared by all instances.
  • Apply static to methods that do not require access to instance-specific data.
  • Utilize static blocks for initializing static variables or executing static initialization code.
About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio