- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
final variables in Java
Final Variables
A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to an different object.
However, the data within the object can be changed. So, the state of the object can be changed but not the reference.
With variables, the final modifier often is used with static to make the constant a class variable.
Example
public class Tester { final int value = 10; // The following are examples of declaring constants: public static final int BOXWIDTH = 6; static final String TITLE = "Manager"; public void changeValue() { value = 12; // will give an error } public void displayValue(){ System.out.println(value); } public static void main(String[] args) { Tester t = new Tester(); t.changeValue(); t.displayValue(); } }
Output
Compiler will throw an error during compilation.
Tester.java:9: error: cannot assign a value to final variable value value = 12; // will give an error ^ 1 error
- Related Articles
- Final static variables in Java
- Why variables are declared final in Java
- Difference between constants and final variables in Java?
- What is blank final variable? What are Static blank final variables in Java?
- Final variables in C#
- Assigning values to static final variables in java\n
- Can we declare final variables without initialization in java?
- Static and non static blank final variables in Java
- Effectively final variables in try-with-resources in Java 9?
- Final local variables in C#
- Interface variables are static and final by default in Java, Why?
- Can a method local inner class access the local final variables in Java?
- final keyword in Java
- Final variable in Java
- Final class in Java

Advertisements