
- 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
How to create a variable that can be set only once but isn't final in Java?
Once you initialize a final variable you cannot modify its value further. i.e. You can assign value to a final variable only once. If you try to assign value to a final variable a compile time error will be generated.
Example
public class FinalExample { final int j = 100; public static void main(String args[]){ FinalExample obj = new FinalExample(); obj.j = 500; System.out.println(obj.j); } }
Compile time error
FinalExample.java:6: error: cannot assign a value to final variable j obj.j = 500; ^ 1 error
Achieving the “final” functionality
To achieve the final functionality without actually using the final keyword −
Make the variable private and set value to it using the setter method such that if you try to invoke it for the second time it should set the previous value or throw an exception.
Example
public class FinalExample { private Integer num; public void setNum(int num){ this.num = this.num == null ? num : this.num; } private String data; public void setData(String data) { this.data = this.data == null ? data : demo(); } public String demo() { String msg = "You cannot set value to the variable data for the second time"; throw new RuntimeException(msg); } public static void main(String args[]){ FinalExample obj = new FinalExample(); obj.setNum(200); System.out.println(obj.num); obj.setNum(500); System.out.println(obj.num); obj.setData("hello"); obj.setData("sample data"); } }
Output
200 200 Exception in thread "main" java.lang.RuntimeException: You cannot set value to the variable data for the second time at SEPTEMBER.remaining.FinalExample.demo(FinalExample.java:15) at SEPTEMBER.remaining.FinalExample.setData(FinalExample.java:12) at SEPTEMBER.remaining.FinalExample.main(FinalExample.java:26)
- Related Articles
- Can a final variable be initialized when an object is created in Java?
- Can we initialize blank final variable in Java
- Final variable in Java
- How to use a final or effectively final variable in lambda expression in Java?
- Can a final class be subclassed in Java?
- Can a constructor be made final in Java?
- final local variable in Java
- What is a final variable in java?
- How many ways are there to initialize a final variable in java?
- Why should a blank final variable be explicitly initialized in all Java constructors?
- Instance variable as final in Java
- Can a class in Java be both final and abstract?
- Can a final keyword alone be used to define a constant in Java?
- Unreachable statement using final variable in Java
- What is a blank uninitialized final variable in java?

Advertisements