Qus:    How to make a read-only class in Java?
May 03, 2021 09:58 1 Answers Views: 14 KRISHNA SWAROOP
Prev Next
Answers (1)
PADMAKEECHU May 04, 2021 13:07
Answer:   We can make a class read-only by making all of the data members private.

Please note:

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.

Post Your Answer
Guest User

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect