静态变量又称类变量,静态方法又称类方法,它们统称为静态成员或类成员。静态成员由static修饰,是属于整个类的,所有的对象共享这些静态成员。不需要创建任何对象,静态成员在类装载时就已初始化了,整个运行期间其内存位置不变,直到类卸载。鉴于静态成员的这些特性,访问静态变量以及定义或调用静态方法时与非静态成员也有不同之处。下面分别做出说明。
1.静态变量
2.静态方法
3.下面以简单的代码验证上面的结论
定义了一个Person类,类的代码见最后面。
(1).类的静态方法可以直接访问静态变量
staticMethod()方法如下:
public static void staticMethod() {
int testY = 20;
System.out.println("She has applied for " + citizenship + " citizenship"); //static variable access
System.out.println("She‘s now " + testY + " years old"); //local variable access
}
main()方法如下:
public static void main(String[] args) {
Person xiaoxi = new Person("xiaoxi",29,"female","piano");
xiaoxi.informationPrint();
staticMethod();
}
输出结果如下:
My name is xiaoxi I am 29 years old I am a girl My hobby is piano I am a chinese She has applied for Chinese citizenship She‘s now 20 years old
结果分析:
(2),静态方法不可以直接访问非静态变量
[1].staticMethod直接访问静态变量citizenship,出现错误
staticMethod()方法如下:
public static void staticMethod() {
int testY = 20;
System.out.println("She has applied for " + citizenship + " citizenship"); //static variable access
System.out.println("She‘s now " + testY + " years old"); //local variable access
System.out.println("She doesn‘t like " + hobby); //nonstatic variable access
}
main方法同上。
输出结果如下:
My name is xiaoxi I am 29 years old I am a girl My hobby is piano I am a chinese Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static field hobby at human.Person.staticMethod(Person.java:99) at human.Person.main(Person.java:107)
结果分析:
[2].staticMethod()创建一个对象testP,由testP访问非静态变量hobby,成功执行
staticMethod()方法如下:
public static void staticMethod() {
int testY = 20;
Person testP = new Person();
System.out.println("She has applied for " + citizenship + " citizenship"); //static variable access
System.out.println("She‘s now " + testY + " years old"); //local variable access
System.out.println("She doesn‘t like " + testP.hobby); // nonstatic variable access via object instance testP
}
main方法同上。
输出结果如下:
My name is xiaoxi I am 29 years old I am a girl My hobby is piano I am a chinese She has applied for Chinese citizenship She‘s now 20 years old She doesn‘t like null
结果分析:
(3),静态方法不可以直接调用非静态方法
[1].staticMethod()直接访问非静态方法informationPrint(),出现错误
staticMethod()方法如下:
public static void staticMethod() {
int testY = 20;
Person testP = new Person();
System.out.println("She has applied for " + citizenship + " citizenship"); //static variable access
System.out.println("She‘s now " + testY + " years old"); //local variable access
//System.out.println("She doesn‘t like " + testP.hobby); //nonstatic variable access
System.out.println("She doesn‘t like " + testP.hobby); // nonstatic variable access via object instance testP
System.out.println("Her personal information is as follows:");
informationPrint();
}
main()方法同上。
输出结果如下:
My name is xiaoxi I am 29 years old I am a girl My hobby is piano I am a chinese Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static method informationPrint() from the type Person at human.Person.staticMethod(Person.java:103) at human.Person.main(Person.java:111)
结果分析:
[2].通过已有对象testP来调用informationPrint()方法,成功执行
staticMethod()方法如下:
public static void staticMethod() {
int testY = 20;
Person testP = new Person();
System.out.println("She has applied for " + citizenship + " citizenship"); //static variable access
System.out.println("She‘s now " + testY + " years old"); //local variable access
System.out.println("She doesn‘t like " + testP.hobby); // nonstatic variable access via object instance testP
System.out.println("Her personal information is as follows:");
//informationPrint();
testP.informationPrint();
}
main()方法同上。
输出结果如下:
My name is xiaoxi I am 29 years old I am a girl My hobby is piano I am a chinese She has applied for Chinese citizenship She‘s now 20 years old She doesn‘t like null Her personal information is as follows: My name is null I am 0 years old Something is wrong! My hobby is null I am a chinese
结果分析:
附Person类:
package human;
public class Person {
String name;
int age;
String gender;
private String hobby;
protected String residence;
static String citizenship = "Chinese";
public Person() {
}
public Person(String n, String g) {
this.name = n;
this.gender = g;
}
public Person(String n, int a, String g, String h) {
this.name = n;
this.age = a;
this.gender = g;
this.hobby = h;
//test:静态变量初始化的时机是否在构造方法之前
System.out.println("constructor:");
System.out.println("change value of the static variable citizenship " + "\"" + citizenship + "\"");
citizenship = "US";
System.out.println(" to " + "\"" + citizenship + "\"");
}
public Person(String n, int a, String g, String h, String r) {
this.name = n;
this.age = a;
this.gender = g;
this.hobby = h;
this.residence = r;
}
public void setName(String n) {
this.name = n;
}
public void setAge(int a) {
this.age = a;
}
public void setGender(String g) {
this.gender = g;
}
public void setHobby(String h) {
this.hobby = h;
}
public void setResidence(String r) {
this.residence = r;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public String getGender() {
return this.gender;
}
public String getHobby() {
return this.hobby;
}
public String getResidence() {
return this.residence;
}
public void informationPrint() {
System.out.println("My name is " + getName());
System.out.println("I am " + getAge() + " years old");
if(getGender() == "female")
System.out.println("I am a girl");
else
if(getGender() == "male")
System.out.println("I am a boy");
else
System.out.println("Something is wrong!");
System.out.println("My hobby is " + hobby);
if(citizenship == "Chinese")
System.out.println("I am a chinese");
//test:静态变量是否在构造方法之前初始化
else
if(citizenship == "US")
System.out.println("I am an American");
else
System.out.println("Oh,something is wrong");
}
public static void staticMethod() {
int testY = 20;
Person testP = new Person();
System.out.println("She has applied for " + citizenship + " citizenship"); //static variable access
System.out.println("She‘s now " + testY + " years old"); //local variable access
System.out.println("She doesn‘t like " + testP.hobby); // nonstatic variable access via object instance testP
System.out.println("Her personal information is as follows:");
//informationPrint();
testP.informationPrint();
}
public static void main(String[] args) {
Person xiaoxi = new Person("xiaoxi",29,"female","piano");
xiaoxi.informationPrint();
staticMethod();
}
}
Java学习笔记八---类的静态变量与静态方法的访问与调用方式
原文:http://www.cnblogs.com/chanchan/p/7707554.html