Java: What modifier makes the object readable outside the class, but not writable?

To make public read-only field, you can make field private and a public getter for this field.

public class Example {
       private  int myExample=1;
       public int getMyExample() {
              return myExample;
       }
}

Leave a Comment