Get your own website
MyClass.java
Person.java
 
public class Main {
  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;
   }
}

                  
Main.java:4: error: name has private access in Person
    myObj.name = "John";
         ^
Main.java:5: error: name has private access in Person
    System.out.println(myObj.name);
                  ^
2 errors