静态变量用于引用所有对象的公共属性(每个对象不是唯一的),例如,员工的公司名称、学生的大学名称等。静态变量在课堂区域中只获取一次内存 类加载时间。 使用静态变量可以让您的程序更节省内存(它可以节省内存)。 静态变量属于类而不是对象。
//Program of static variable
class Student8 {
int rollno;
String name;
static String college = "ITS";
Student8(int r, String n) {
rollno = r;
name = n;
}
void display() {
System.out.println(rollno + " " + name + " " + college);
}
public static void main(String args[]) {
Student8 s1 = new Student8(111, "Karan");
Student8 s2 = new Student8(222, "Aryan");
s1.display();
s2.display();
}
}
输出结果如下:
111 Karan ITS
222 Aryan ITS