
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a variable that can be set only once but isn\\\'t final in Java?
How to create a variable that can be set only once, meaning it can be assigned a value only once but is not final in Java?
Solution
In Java we can solve the above using the following two methods:
Using a custom wrapper class
We can create a custom wrapper class that allows setting a value only once. The class will have a boolean flag to check if the value has already been set. Wrapper classes are used for converting primitive data types into objects. We will use this for setting the value only once. Let's look at the steps:
- Create a class with a private variable to store the value.
- Add a boolean variable to remember if the value is already set.
- Make a method to set the value. In this method, check if the value is already set. If yes, show an error.
- Make a method to get the value.
- In the main method, make an object of this class and set the value using the setter method.
- Try to set the value again. This should show an error.
Example
In the following example, we will create a custom wrapper class that allows us to set a value only once. The class will have a boolean flag to check if the value has already been set.
Following is the Java program to create a variable that can be set only once but is not final:
public class SetSingleValue { private String val; private boolean isSet = false; public void setVal(String val) { if (isSet) { System.out.println("Value is already set. Cannot set it again."); return; } this.val = val; isSet = true; } public String getVal() { return val; } public static void main(String[] args) { SetSingleValue obj = new SetSingleValue(); obj.setVal("Hello"); System.out.println("Value set: " + obj.getVal()); obj.setVal("World"); // This should show an error System.out.println("Value set: " + obj.getVal()); } }
Following is the output of the above code:
Value set: Hello Value is already set. Cannot set it again. Value set: Hello
Using an AtomicReference
We can use an AtomicReference class that provides a way to create a variable that can be set only once. The class has a method called compareAndSet()
that allows us to set the value only if it is not already set. Let's look at the steps:
- Make an AtomicReference object.
- Use the compareAndSet() method to set the value. This method checks if the current value is what you expect. If yes, it sets the new value and returns true. If not, it does nothing and returns false.
- This way, you can make sure the value is set only once.
Example
We will take a string "Hello" and try to set it to "World". The first time it will set the value, but the second time it will not set the value and return false. Following is the Java program to create a variable that can be set only once but is not final using AtomicReference:
import java.util.concurrent.atomic.AtomicReference; public class AtomicReferenceExample { public static void main(String[] args) { AtomicReference<String> atomicRef = new AtomicReference<>(); boolean isSet = atomicRef.compareAndSet(null, "Hello"); System.out.println("Value set: " + isSet); isSet = atomicRef.compareAndSet(null, "World"); // This should not set the value System.out.println("Value set: " + isSet); System.out.println("Current value: " + atomicRef.get()); } }
Following is the output of the above code:
Value set: true Value set: false Current value: Hello