1.修饰变量;
public class static001 {
public static void main(String[] args){
Student sc = new Student("黄鱼",20);
Student.room = "01教室";
System.out.println(sc.getName()+","+sc.getAge()+","+sc.room);
Student sc2 = new Student("贱贱",21);
System.out.println(sc2.getName()+","+sc2.getAge()+","+sc.room);
}
}
class Student{
private String name;
private int age;
static String room;//共有的变量;
public Student(){
}
public Student(String name,int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2:修饰方法:
public class static002 {
public static void main(String[] args){
StaticMc01 a = new StaticMc01();//先创建对象;
a.method();//然后才能使用没有static关键字的内容;
a.methodStatic();//通过对项目进行调用。 不推荐。
StaticMc01.methodStatic();//直接通过类名称调用。 推荐。
method01();//对于本类的静态方法来说,可以省略类名称;
}
public static void method01(){
System.out.println("这是一个本类的静态方法");
}
}
class StaticMc01{
public void method(){
System.out.println("这是一个成员方法");
}
public static void methodStatic(){
System.out.println("这是一个静态方法");
}
}
3:修饰静态代码块;
public class static003 {
public static void main(String[] args) {
person one = new person();
}
}
class person{
static {
System.out.println("静态代码块");
}
public person(){
System.out.println("构造方法");
}
}



