Why we need static?
We have konwn new can create a new object and system will locate new memory to this object, but sometimes we hope some data can keep unchange, which means we only need one memory to store this attribute.
eg, all students have the same school name, so ,when all student in one school will only need one school name. How to make all students share the school name?
static can refer variable or method which do not rely on specific object
System will only allocate memory when create the first object. which means if we new another object, system will not allocate memory for this static variable or staic method.And all object share the memory.
the static member can be called by class name or object name.
the lifelong of static is same with class.
静态方法不能直接访问所属类中的非静态变量和非静态方法
public static String schoolName = "SD";
System.out.print(james.schoolName);
System.out.print(Student.schoolName);
解释main 方法
public static void main(String[] args)
public:由于JVM需要调用main方法,因此该方法的权限必修是public.
static:main方法是静态方法,JVM创建main方法的时间较早,不依赖于对象。main方法不能直接访问类中的非静态成员,因此需要创建实例对象,通过对象访问非静态成员。JVM 在执行main方法时,不必创建对象,所以方法必须是static.
void 没有返回值
String[]:该数组保存执行java命令时传递给运行类的参数