public class MyClass {
public static void main(String[] args) {
Person myObj = new Person();
myObj.name = "John";
System.out.println(myObj.name);
}
}
public class Person {
private String name;
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
this.name = newName;
}
}
Result:
MyClass.java:4: error: name has private access in Person
myObj.name = "John";
^
MyClass.java:5: error: name has private access in Person
System.out.println(myObj.name);
^
2 errors