java 实例变量在整个类内部是可访问的,而不管实例变量声明在类的哪个位置。
比如在下面代码中,尽管实例变量声明在类的尾部,在之前方法中仍可访问。
public class Employee{
public Employee (String empName){
name = empName;
}
public void setSalary(double empSal){
salary = empSal;
}
public void printEmp(){
System.out.println("name:" + name);
System.out.println("salary:" + salary);
}
public static void main(String args[]){
Employee empOne = new Employee("RUNOOB");
empOne.setSalary(1000);
empOne.printEmp();
System.out.println(empOne.salary);
}
public String name;
private double salary;
}