
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Difference between constants and final variables in Java?
Constant in Java
A constant variable is the one whose value is fixed and only one copy of it exists in the program. Once you declare a constant variable and assign value to it, you cannot change its value again throughout the program.
Unlike in C language constants are not supported in Java(directly). But, you can still create a constant by declaring a variable static and final.
Once you declare a variable static they will be loaded in to the memory at the compile time i.e. only one copy of them is available.
once you declare a variable final you cannot modify its value again.
Example
class Data { static final int integerConstant = 20; } public class ConstantsExample { public static void main(String args[]) { System.out.println("value of integerConstant: "+Data.integerConstant); } }
Output
value of integerConstant: 20
Final variable in Java
Once you declare a variable final you cannot change its value. If you try to do so a compile time error will be generated.
Example
public class FinalExample { public static void main(String args[]) { final int num = 200; num = 2544; } }
Output
FinalExample.java:4: error: cannot assign a value to final variable num num = 2544; ^ 1 error
The main difference between final variable and a constant (static and final) is that if you create a final variable without static keyword, though its value is un-modifiable, a separate copy of the variable is created each time you create a new object. Where a constant is un-modifiable and have only one copy through out the program. For example, consider the following Java program,
Example
class Data { final int integerConstant = 20; } public class ConstantExample { public static void main(String args[]) { Data obj1 = new Data(); System.out.println("value of integerConstant: "+obj1.integerConstant); Data obj2 = new Data(); System.out.println("value of integerConstant: "+obj2.integerConstant); } }
Output
value of integerConstant: 20 value of integerConstant: 20
Here we have created a final variable and trying to print its value using two objects, thought value of the variable is same at both instances, since we have used a different object for each they are the copies of the actual variable.
According to the definition of the constant you need to have a single copy of the variable throughout the program (class).
Therefore, to create constant as pert definition, you need to declare it both static and final.
- Related Articles
- What is the difference between constants and variables?
- Difference Between Static and Final in Java
- final variables in Java
- Difference Between Final, Finally and Finalize in Java
- Difference between C++ string constants and character constants
- Final static variables in Java
- Static and non static blank final variables in Java
- What is the difference between class variables and instance variables in Java?
- Why variables are declared final in Java
- What is the difference between final, finally and finalize() in Java?
- How to create Variables and Constants in C++?
- Interface variables are static and final by default in Java, Why?
- What is blank final variable? What are Static blank final variables in Java?
- Final variables in C#
- Difference Between RTM and Final Windows 7
