The primary data that must in some way be represented for each object is the instance variables declared in the objectís class and all its superclasses.
实例变量:用来保存对象的重要数据,存放在堆(heap)中。
哪么它是什么时候进行初始化呢?
一、描述一、
实例变量:也可以说是一个对象的属性
1》值是一个对象,表示包含它的对象可以和这个对象联系。
例如:人对象,有属性:联系其他人。哪么人和人可以通过“联系其他人”这个属性,相互连接,
形成社交网。
例子;1、YelloFish 只有一个实例变量myfiled,
即 YelloFish 有 一个属性:可以连接对象,类型是自己,表示只能和YelloFish 的对象相连。
2、BlueFish 有2个实例变量(2个连接属性)
BlueFish myFried:可以连接自己对象
YelloFish myLunch:也可以连接YelloFish 建立的对象。
3、RedFish 有3个实例变量(3个连接属性)
RedFish myFried:可以连接自己对象
BlueFish myLunch:也可以连接BlueFish 建立的对象。
YelloFish mySnack:也可以连接YelloFish 建立的对象。
二、实例变量的在堆中的分配空间,只有连接属性,一个连接变量分配4个 字节。
YelloFish =4个字节
BlueFish =8个字节
RedFish =12个字节
| package gc.ex1; public class YelloFish { YelloFish myfiled; } package gc.ex1; public class BlueFish { BlueFish myFried; YelloFish myLunch; } package gc.ex1; public class RedFish { RedFish myFried;//实例成员变量 BlueFish myLunch; YelloFish mySnack; } package gc.ex1; public class testfish { public static void main(String[] args) { // 表示对象建立,objref 放在 Local Variables 中。 YelloFish yf = new YelloFish(); BlueFish bf = new BlueFish (); RedFish rf= new RedFish (); // YelloFish droppedFish = new YelloFish(); // // Sometime later the assignment takes place //两个对象通过属性myfiled相连。 yf.myfiled = droppedFish; new YelloFish(); } } LocalVariableTable: Start Length Slot Name Signature 0 46 0 args [Ljava/lang/String; 8 38 1 yf Lgc/ex1/YelloFish; 16 30 2 bf Lgc/ex1/BlueFish; 24 22 3 rf Lgc/ex1/RedFish; 33 13 4 droppedFish Lgc/ex1/YelloFish; |
二、分配空间:
1、格式:1>an object reference:对应Local variables表
2>the handle pool:就是存放两指针。
当new 一个对象,jvm估计要多少内存空间,哪么建立一个 the handle pool----再去 the object pool 根据head 查看,是否空间是空闲,大小够不够。
3>the object pool:
每一横条:是4个字节大小,两部分组成(1>head 存放这个块size,是否空闲,2>才是真正的block)
1、main() 建立对象:下面是一个对象连接网,垃圾收集也靠它。
new YelloFish():没有连接指针指向它,本地变量表里也没有,如下面的单个鱼,和谁也不连,但是堆中还是分配了空间,垃圾收集器会首先收走。
yf(obj reference):因此能快速的和对象连接,
2、下面是对象在堆中的分配情况。