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;
}
}
More Related Contents:
- What is the difference between public, protected, package-private and private in Java?
- Understanding java’s protected modifier
- What does the “static” modifier after “import” mean?
- When overriding a method, why can I increase access but not decrease it?
- What is the default access modifier in Java? [duplicate]
- Pros and cons of package private classes in Java?
- Static block in Java not executed
- Can non-static methods modify static variables
- Isn’t “package private” member access synonymous with the default (no-modifier) access?
- Modifier Keyword order in Java
- Protected access modifier in Java
- Default access modifier for a Java constructor
- Why subclass in another package cannot access a protected method?
- Using a private variable in a inherited class – Java
- Why use the `transient` keyword in java? [duplicate]
- Call protected method from a subclass of another instance of different packages
- Why can’t my subclass access a protected variable of its superclass, when it’s in a different package?
- How to parse date string to Date? [duplicate]
- How to retrieve element value of XML using Java?
- Using BigDecimal to work with currencies
- How do shift operators work in Java? [duplicate]
- In Java, what are the advantages of streams over loops? [closed]
- Transitive dependencies not resolved for aar library using gradle
- Getting the difference between two sets
- How to set up default schema name in JPA configuration?
- Two dimensional array list
- Are Thread.stop and friends ever safe in Java?
- How to create a secure random AES key in Java?
- Prevent JFreeChart DialPlot wrapping around with large values?
- How to make PopUp window in java