java类全局变量、对象全局变量、类方法、对象方法、构造方法的区别
package lianxi;
public class test11_4 {
//常量不会改变
public static final int NUM=2;
//类全局变量,要用static来修饰
static int b=10;
//对象全局变量
int a =22;
//对象全局变量
String name="张三";
//对象方法void 代表没有返回值
public void info() {
System.out.println(this.a+"++"+this.name);
}
//类方法
public static void sleep() {
}
//构造方法 默认存在
public test11_4(String name,int a) {
this.name =name;
this.a=a;
}
public static void main(String[] args) {
int c =30;//局部变量
//新建对象t1
test11_4 t1=new test11_4("qcby",3);
//新建对象t2
test11_4 t2=new test11_4("qc",4);
//对象全局变量 用t1可以直接调用
System.out.println(t1.a);
System.out.println(t1.name);
System.out.println(test11_4.NUM);
//类全局变量 用类名test11_4调用
System.out.println(test11_4.b);
//执行对象方法info(),类方法用test11_4调用
t1.info();
t2.info();
}
}