/*
- @author GZP @email:g18916986264@163.com
- @data 2021-06-19 11:17 */
1、当我们创建Person对象时,首先会去方法区查找是否有Person类信息
2、①如果方法区中没有Person类信息,那么便会进行类加载(JVM会把对应的字节码文件加载 到方法区中)
②在加载类信息时会为类变量开辟内存区域
3、如果方法区中已经存在Person类信息,那么直接创建对象
//需求:让id实现自增
class Employee{
int id;
String name;
int age;
static int num=0;
public Employee(String name,int age){
this.name=name;
this.age=age;
id=++num;
}
public void show(){
System.out.println("id="+id +"name="+name +"age="+age);
}
}
//需求:统计computer创建对象的个数
class Computer{
static int count=0;
public Computer(){
count++;
}
public void show(){
System.out.println("count="+count);//count=6
}
}
public class StaticTest2 {
public static void main(String[] args) {
new Computer();
new Computer();
new Computer();
new Computer();
new Computer();
Computer c=new Computer();
c.show();
System.out.println("------------------------");
Employee e=new Employee("aa",16);
Employee e1=new Employee("bb",17);
Employee e2=new Employee("cc",18);
Employee e3=new Employee("dd",19);
e.show();
e1.show();
e2.show();
e3.show();
}
}