Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between static and final in Java

Difference Between static and final in Java

March 9, 2018 Posted by Lithmee

Key Difference – static vs final in Java
 

Each programming language has a specific syntax. The programmer should follow these syntaxes when writing programs. The keywords of programming languages have specific meanings according to the tasks. They are provided by the programming language and cannot be used for user-defined variables, methods, classes, etc. The static and final are two keywords in Java. This article discusses the difference between static and final in Java. The key difference between static and final in Java is that static is used to define the class member that can be used independently of any object of the class while final is used to declare a constant variable or a method that cannot be overridden or a class that cannot be inherited.

CONTENTS

1. Overview and Key Difference
2. What is static in Java
3. What is final in Java
4. Similarities Between static and final in Java
5. Side by Side Comparison – static vs final in Java in Tabular Form
6. Summary

What is static in Java?

A class consists of data members (attributes) and methods. In order to call the methods, there should be an object of that specific class. When a method is declared as static, it is not needed to create an object to call that method. The method can be called using the class name. Refer the below program.

Difference Between static and final in Java

Figure 01: Java Program with static variables and static Method

According to the above program, class A contains number variable and display method. Both are static members. Therefore, it is not necessary to create an object to access the number variable and display method. The programmer can directly write the class name to print the number and to call the method display. So, there is no need to instantiate an object.  If the number variable and display method are non-static, then there should be an object of type A.

Difference Between static and final in Java_Figure 02

Figure 02: Use of static Block

The above program contains the static block and the main method. The static block is called when the class is loaded. Therefore, the statement in the static block executes before the statement in the main block.  If there are many static blocks, they will execute in sequence.

What is final in Java?

In the program, there can be variables of various types. If there is a variable as int x=1; later in the program, that variable value can be changed to some other value. A variable that is declared as final and initialized with a value cannot be changed later in the program.

Difference Between static and final in Java_Figure 03

Figure 03:  Program with final Variable and Inheritance

According to the above program, x is a final variable. It is assigned a value 5. It cannot be changed some other value because it is declared as final. Java supports Object-oriented programming (OOP). One pillar of OOP is a polymorphism. One type of polymorphism is overriding. Class A has the display method. The class B extends class A and it has its own display method. When creating an object of type B and calling the display method will print “B” as the output. The display method of class A is overridden by the display method of class B.

If the programmer what to avoid overriding a method, then he can use the final keyword for that method. If the display method in class A is final, the display method in B will give an error because that method cannot be overridden.

Difference Between static and final in Java_Figure 04

Figure 04: final keyword in the Method

Another pillar of OOP is inheritance. It helps to reuse the already existing code. The new class can extend from the existing class and use the data members and methods of the existing class. If it is required to stop inheriting a class, the programmer can use the keyword ‘final’. Refer the below program.

Ke3y Difference Between static and final in Java

Figure 05: final keyword in the Class

According to the above program, class A is declared as final. When class B extends A, it gives an error because class A is declared as final. It cannot be inherited by other classes.

What is the Similarity Between static and final in Java?

  • Both static and final are keywords in Java.

What is the Difference Between static and final in Java?

static vs final in Java

Static keyword denotes that a member variable, or method, can be accessed without requiring an instantiation of the class to which it belongs. The final keyword denotes an entity that can only be assigned once.
 Variables
The static variables can be reinitialized. The final variables cannot be reinitialized.
Methods
Can be called by other static methods and only access the static members of the class. The final methods cannot be overridden.
Class
The static class object cannot be created. It only contains static members only. The final class cannot be inherited by other classes.
Block
The static keyword can be used in a block. The final keyword is not used with a block.

Summary – static vs final in Java

This article discussed two keywords in Java such as static and final. The difference between static and final in Java is that static is used to define the class member that can be used independently of any object of the class while final is used to declare a constant variable or a method that cannot be overridden or a class that cannot be inherited. 

Reference:

1.What is Static Keyword in Java | static method and static variable, Telusko Learnings, 6 Mar. 2015. Available here  
2.7.16 How to use Static Block in Java Tutorial, Telusko Learnings, 30 Apr. 2015. Available here
3.8.13 How to use Final Keyword in Java | Method , class and variable, Telusko Learnings, 26 Feb. 2015. Available here  

Related posts:

Difference Between final finally and finalize in JavaDifference Between final finally and finalize in Java Key Difference Between getch and getcheDifference Between getch and getche Key Difference Between Machine Learning and Artificial IntelligenceDifference Between Machine Learning and Artificial Intelligence Difference Between Header File and Library FileDifference Between Header File and Library File Difference Between int and longDifference Between int and long

Filed Under: Programming Tagged With: Compare static and final in Java, final in Java, final in Java Block, final in Java Class, final in Java Definition, final in Java Methods, final in Java Variables, static and final in Java Differences, static and final in Java Similarities, static in Java, static in Java Block, static in Java Class, static in Java Definition, static in Java Methods, static in Java Variables, static vs final in Java

About the Author: Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a Reply Cancel reply

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

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Netflix and Zune

Difference Between Lenovo IdeaPad Tablet K1 and Thinkpad Tablet

Difference Between Scientist and Researcher

Difference Between Crow and Raven

Difference Between Essay and Short Story

Difference Between Essay and Short Story

Latest Posts

  • What is the Difference Between COPD and Lung Cancer
  • What is the Difference Between Silk and Cotton
  • What is the Difference Between Galactosemia and Lactose Intolerance
  • What is the Difference Between Glycosuria and Glucosuria
  • What is the Difference Between Menkes and Wilson’s Disease
  • What is the Difference Between Xerophthalmia and Keratomalacia
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.