java内存分配实例

在java内存分配中,静态变量是在运行时JVM只为静态变量分配一次内存,在加载类的过程中完成静态变量的内存分配。

静态变量只属于某个类,不属于对象,所以通过类名访问。而不是静态变量就需要创建对象,然后再分配内存空间,再通过内存名去访问。

public class Student{
public String name;
public static int studentCount;
public final byte MAX_AGE=30;

public static void main(String[]arg) {
System.out.println(Student.studentCount);
Student stu=new Student();
System.out.println(stu.name);
System.out.println(stu.MAX_AGE);
}
}

猜你喜欢

转载自www.cnblogs.com/fly0512/p/9860689.html