A class with private
attributes:
public class Main {
private String fname = "John";
private String lname = "Doe";
private String email = "john@doe.com";
private int age = 24;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println("Name: " + myObj.fname + " " + myObj.lname);
System.out.println("Email: " + myObj.email);
System.out.println("Age: " + myObj.age);
}
}
The private
keyword is an access modifier used for attributes, methods and constructors, making them only accessible within the declared class.
Read more about modifiers in our Java Modifiers Tutorial.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!