static
package com.opp.Demo06;
public class Student {
static int age;//static可以定义在成员变量,但是不可用于局部变量上
double score;
public static void main(String[] args) {
//static int i = 0; 是错误的,不可用于局部变量
Student s1 = new Student();
System.out.println(s1.score);
System.out.println(s1.age);
System.out.println(Student.age); //调用参数还可以用类调用,但是前提是静态参数
// System.out.println(Student.score); 类调用非静态参数会报错
}
}
package com.opp.Demo06;
//静态和非静态方法
public class Student {
public void run(){//非静态
go();//非静态对静态的调用直接用。
}
public static void go(){//静态
}
public static void main(String[] args) {//主方法是静态的,直接调用其他方法必须是静态的
new Student().run();//对非静态的调用,用new 对象.方法
go(); //静态对静态的调用直接用。
}
}
package com.opp.Demo07;
public class Person {
{
System.out.println("匿名代码块"); //2 用于赋初始值
}
static{
System.out.println("静态代码块"); //1 静态区域先执行而且只执行一次;用于初始化动作
}
public Person() {
System.out.println("构造方法"); //3
}
public static void main(String[] args) {
Person person1 = new Person();
System.out.println("===============");
Person person2 = new Person();
}
//运行结果:静态代码块
//匿名代码块
//构造方法
//===============
//匿名代码块
//构造方法
}
浙公网安备 33010602011771号