

package com.java.test;
public class T {
//类变量,静态变量
public static String s="srx";
public String s1="zf";
public static void main(String args[]){
System.out.println("s=="+s);
System.out.println("T.s=="+T.s);
//通过类可以调用,通过实例更可以调。newy一个东西,会占用内存空间
T t= new T();
T t1= new T();
t1.s="heihei";
//验证一个类中的不同实例的static变量共享一个内存空间,下面这个t1.s给s赋值,把static类型String s已经改变了,所以下面所有的打印出来的都是改变后的s值。
System.out.println("t.s=="+t.s+",t1.s=="+t1.s+",T.s=="+T.s);
a();
T.a();
t.b();
}
public static void a(){
System.out.println("a()方法");
}
public void b(){
System.out.println("b()T.s=="+T.s);;
}
}
public class T {
//类变量,静态变量
public static String s="srx";
public String s1="zf";
public static void main(String args[]){
System.out.println("s=="+s);
System.out.println("T.s=="+T.s);
//通过类可以调用,通过实例更可以调。newy一个东西,会占用内存空间
T t= new T();
T t1= new T();
t1.s="heihei";
//验证一个类中的不同实例的static变量共享一个内存空间,下面这个t1.s给s赋值,把static类型String s已经改变了,所以下面所有的打印出来的都是改变后的s值。
System.out.println("t.s=="+t.s+",t1.s=="+t1.s+",T.s=="+T.s);
a();
T.a();
t.b();
}
public static void a(){
System.out.println("a()方法");
}
public void b(){
System.out.println("b()T.s=="+T.s);;
}
}
输出结果如下:
/*打印结果
* s==srx
T.s==srx
t.s==heihei,t1.s==heihei,T.s==heihei
a()方法
a()方法*/
第二个:


package com.java.test;
public class T1 {
//类变量,静态变量
public static String s="srx";
public String s1="zf";
public static void main(String args[]){
//一个类的静态方法能够直接(不用new实例)调用静态变量。
System.out.println("s=="+s);
/*一个类的静态方法不能够直接访问非静太变量,只能new一个类之后,用实例调用非静态属性
* Cannot make a static reference to the non-static field s1
System.out.println("非静态s1=="+s1);*/
/*Cannot make a static reference to the non-static field T1.s1
System.out.println("T1.s1=="+T1.s1);*/
System.out.println("T.s=="+T1.s);
//通过类可以调用,通过实例更可以调。newy一个东西,会占用内存空间
T1 t= new T1();
System.out.println("t.s1=="+t.s1);
T1 t1= new T1();
t1.s="heihei";
//验证一个类中的不同实例的static变量共享一个内存空间,下面这个t1.s给s赋值,把static类型String s已经改变了,所以下面所有的打印出来的都是改变后的s值。
System.out.println("t.s=="+t.s+",t1.s=="+t1.s+",T1.s=="+T1.s);
/*这里不能用this
//this.
*/
t.b();
}
public static void a(){
System.out.println("a()方法");
}
public void b(){
/*//在这里可以用this.
this.*/
System.out.println("b()T1.s=="+s);
a();
T1 t3= new T1();
t3.a();
c();
t3.c();
System.out.println("b()T1.s1=="+s1);
System.out.println("b()T1.t3.s1=="+t3.s1);
}
public void c(){
System.out.println("c()");
}
}
public class T1 {
//类变量,静态变量
public static String s="srx";
public String s1="zf";
public static void main(String args[]){
//一个类的静态方法能够直接(不用new实例)调用静态变量。
System.out.println("s=="+s);
/*一个类的静态方法不能够直接访问非静太变量,只能new一个类之后,用实例调用非静态属性
* Cannot make a static reference to the non-static field s1
System.out.println("非静态s1=="+s1);*/
/*Cannot make a static reference to the non-static field T1.s1
System.out.println("T1.s1=="+T1.s1);*/
System.out.println("T.s=="+T1.s);
//通过类可以调用,通过实例更可以调。newy一个东西,会占用内存空间
T1 t= new T1();
System.out.println("t.s1=="+t.s1);
T1 t1= new T1();
t1.s="heihei";
//验证一个类中的不同实例的static变量共享一个内存空间,下面这个t1.s给s赋值,把static类型String s已经改变了,所以下面所有的打印出来的都是改变后的s值。
System.out.println("t.s=="+t.s+",t1.s=="+t1.s+",T1.s=="+T1.s);
/*这里不能用this
//this.
*/
t.b();
}
public static void a(){
System.out.println("a()方法");
}
public void b(){
/*//在这里可以用this.
this.*/
System.out.println("b()T1.s=="+s);
a();
T1 t3= new T1();
t3.a();
c();
t3.c();
System.out.println("b()T1.s1=="+s1);
System.out.println("b()T1.t3.s1=="+t3.s1);
}
public void c(){
System.out.println("c()");
}
}
输出:
s==srx
T.s==srx
t.s1==zf
t.s==heihei,t1.s==heihei,T1.s==heihei
b()T1.s==heihei
a()方法
a()方法
c()
c()
b()T1.s1==zf
b()T1.t3.s1==zf
总结:
1、静态里:可以直接用静态的;想用非静态的,先new
2、非静态里:用静态的时候: 可以直接用静态的,也可用实例.静态的,
用非静态的时候:可以直接用非静态的
详细说明:
1、静态里:可以直接用静态的(属性或者方法);想用非静态的,先new,
2、非静态里:用静态的时候: 可以直接(或对象名.静态的(属性或者方法))用静态的,也可用实例(new).静态的,
用非静态的时候:可以直接(不用new)用非静态的,也可以new后,调用非静态的(属性或者方法)