Qus: How to make a read-only class in Java?
If we make a class read-only, then we can’t modify the properties or data members value of the class.
If we make a class read-only, then we can only read the properties or data members value of the class.
The read-only class will contain only getter methods which return the value of the private properties to the main() function.
The read-only class can contain setter methods if we want to modify the value of the private properties after reading because there is our choice to keep setter method in the class but as per based on the concepts we should not contain.
Now, we will see the objective of the getter method, why it is required?
Few points need to remember about getter methods are given below:
As we know that "private" data member of the class is accessible in the same class only.
Let suppose we want to access "private" data member of the class in outside class. So, in that case, we need to declare public "getter" methods.
The objective of the getter method is used to view the private variable values.