0 votes
in JAVA by
How to make a read-only class in Java?

1 Answer

0 votes
by
A class can be made read-only by making all of the fields private. The read-only class will have only getter methods which return the private property of the class to the main method. We cannot modify this property because there is no setter method available in the class. Consider the following example.

  //A Java class which has only getter methods.    

public class Student{    

//private data member    

private String college="AKG";    

//getter method for college    

public String getCollege(){    

return college;    

}    

}

Related questions

+2 votes
asked May 6, 2021 in JAVA by SakshiSharma
0 votes
asked Oct 10, 2020 in JAVA by Robindeniel
...