如果你还不了解JVM内存模型的建议您先看下JVM内存模型
以一下代码为例,来分析下,java的实例对象在内存中的空间分配(JDK1.8)。
public class Student {
private String name;
private static Birthday birthday = new Birthday();
public Student(String name) {
this.name = name;
}
public static void main(String[] args) {
Student s = new Student("zhangsan");
int age = 10;
System.out.println(age);
}
}
class Birthday {
private int year = 2010;
private int month = 10;
private int day = 1;
}
以Student类执行到main方法的最后一行时来分析java实例对象在内存中的分配情况。
如下图:
java对象在内存中的关系
图画的稍微有点问题,不过能说明对象在内存中的大致位置。
从图中我们可以看出,普通的java实例对象内存分配,主要在这三个区域:虚拟机栈、堆、方法区。
通过变量的角度来分析,我们就可以了解为什么静态变量不用new就能调用,而实例变量必须new出对象,才能调用。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有