//第一步:编写类
class Student{
//第二步:编写方法(成员变量或叫实例变量)
String name;//默认值为null
int age;//默认值为0
char sex;//默认值为/u0000
String address;//默认值为null
//第三部:编写方法
public void study(){
System.out.println("正在创建对象");
}
}
class StudentTest{
public static void main(String args[]{
//语法格式:类名 引用名=new 类名();
//new Student 表示创建了一个对象,同时在堆内存中开辟一块空间,cd=new Student();将右侧的对象地址赋给左侧引用
//注意此处并不是指针
Student cd=new Student();
}}
1.因为主方法main()在StudentTest中,所以运行StudentTest生成字节码文件,StudentTest.class放在方法区中。
2.从StudentTest中找到主方法main(),因为args是主方法的形参,所以在栈内存中开辟一块空间。
3.在主方法中找到Student cd=new Student();从右向左开始,new Student表示创建了一个对象,同时在堆内存中开辟一块地址(地址是随机分配的,我们假设这块地址是0x1234),新创建的对象(实例变量)中有四个属相:name age sex address,在新创建的这个对象种给这些属性分配空间。Student cd=new Student();继续向右进行,找到cd,则在栈内存中给变量cd开辟一块空间,并将右侧对象地址赋给左侧引用。