Public keyword in Java

Public keyword in Java is an access modifier. Public keyword in java means the following element is available to everyone. like classes attributes methods and constructions

Example of Public keyword in java


public class Profile{
  public String fname = "John";
  public String lname = "Doe";
  public String email = "[email protected]";
  public int age = 24;
}

class Second {
  public static void main(String[] args) {
    Profile myObj = new Profile();
    System.out.println("Name: " + myObj.fname + " " + myObj.lname);
    System.out.println("Email: " + myObj.email);
    System.out.println("Age: " + myObj.age);
  }
}


Leave a Comment