(注:静态变量修改为静态成员变量,静态方法改为静态成员方法)

静态成员变量又称类变量,静态成员方法又称类方法,它们统称为静态成员或类成员。静态成员由static修饰,是属于整个类的,所有的对象共享这些静态成员。不需要创建任何对象,静态成员在类装载时就已初始化了,整个运行期间其内存位置不变,直到类卸载。鉴于静态成员的这些特性,访问静态成员变量以及定义或调用静态成员方法时与非静态成员也有不同之处。下面分别做出说明。

1.静态成员变量

  • 类的非静态成员方法、静态成员方法都可以直接访问静态成员变量
  • 其他类要访问某个类的静态成员变量,既可以通过实例名访问,也可以直接用类名来访问,推荐用类名访问的方式,这样能更直观的说明访问的变量是静态成员变量

2.静态方法

  • 不能直接访问非静态成员变量,也不能直接调用非静态成员方法,需要实例化一个对象,再通过该对象来访问要访问的非静态成员变量或要调用的非静态成员方法。也就是说,静态成员方法不能直接使用非静态成员。个人理解是,非静态成员变量是依托对象而存在的,当没有实例一个对象时,非静态成员变量是没有分配内存空间的,静态方法要使用非静态成员变量不知道要到哪里去找,当然就不能直接使用非静态成员变量了。而非静态成员方法有有可能访问非静态成员变量,所以也不能直接调用非静态成员方法了。
  • 其他类要调用某个类的静态成员方法,既可以通过实例名调用,也可以直接用类名来调用,推荐用类名调用的方式,这样能更直观的说明调用的方法是静态成员方法

3.下面以简单的代码验证上面的结论

定义了一个Person类,类的代码见最后面。

(1).类的静态成员方法和非静态成员方法都可以直接访问静态成员变量

  • 静态成员方法staticMethod访问了静态成员变量citizenship
  • 定义并使用了局部变量testY
  • 非静态成员方法informationPrint访问了静态成员变量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
}

informationPrint()方法如下:

	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");
else
if(citizenship == "US")
System.out.println("I am an American");
else
System.out.println("Oh,something is wrong");
} 

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访问了静态成员变量citizenship
  • 定义并使用了局部变量testY
  • 访问了非静态成员变量hobby

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)

结果分析:

  • 静态成员方法不能直接访问非静态成员变量,否则会出现“Cannot make a static reference to the non-static field hobby” 的错误。

 [2].staticMethod()创建一个对象testP,由testP访问非静态成员变量hobby,成功执行

  • 静态成员方法staticMethod使用了静态成员变量citizenship
  • 定义并使用了局部变量testY
  • 创建一个Person实例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使用了静态成员变量citizenship
  • 定义并使用了局部变量testY
  • 创建一个Person实例testP,并使用了testP的hoppy变量
  • 直接调用非静态成员方法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)

结果分析:

  • 静态成员方法不能直接调用非静态成员方法,否则会出现与直接访问非静态成员变量类似的错误,Cannot make a static reference to the non-static method informationPrint() from the type Person。

 [2].通过已有对象testP来调用informationPrint()方法,成功执行

  • 静态成员方法staticMethod使用了静态成员变量citizenship
  • 定义并使用了局部变量testY
  • 创建一个Person实例testP,并使用了testP的hoppy变量
  • 通过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

结果分析:

  • 静态成员方法要调用非静态成员方法,可以先实例化一个对象,再通过该对象来调用。

(4).其他类通过类名直接调用类的静态成员变量和静态成员方法

  • 类TestMain通过类名Person直接调用了静态成员方法staticMethod()和静态成员变量citizenship

TestMain如下:

package human;

public class TestMain {

	public static void main(String[] args) {
Person.staticMethod();
System.out.println("static variable \"citizenship\":" + Person.citizenship); } }

输出结果如下:

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
static variable "citizenship":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");
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学习笔记8---类的静态成员变量与静态成员方法的访问与调用方式的更多相关文章

  1. Java学习笔记之---类和对象

    Java学习笔记之---类和对象 (一)类 类是一个模板,它描述一类对象的行为和状态  例如:动物类是一个类,动物们都有属性:颜色,动物们都有行为:吃饭 public class Dog { Stri ...

  2. Java学习笔记——File类之文件管理和读写操作、下载图片

    Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图 ...

  3. 0018 Java学习笔记-面向对象-类的基本要素

    类与对象 大街上一个个的人,就是一个个对象 类是对一群对象的抽象,比如人都有性别.年龄.姓名,都会吃饭.睡觉等.姓名性别可以抽象为变量,吃饭睡觉可以抽象为方法,像下面一样定义个类来形容人 public ...

  4. java学习笔记(六):变量类型

    java一共三种变量: 局部变量(本地变量):方法调用时创建,方法结束时销毁 实例变量(全局变量):类创建时创建,类销毁时销毁 类变量(静态变量):程序启动是创建,程序销毁时销毁 public cla ...

  5. Java学习笔记-枚举类

    实例有限且固定的类成为枚举类 枚举类的实现 早期时候的实现形式: public static final int SEASON_SPRING = 1; public static final int ...

  6. Java基础笔记(七)—— 成员变量、静态变量、局部变量

    public class Test { int c; //成员变量(实例变量) static int s1; //静态变量(类变量)(全局变量) public static void main(Str ...

  7. Java学习笔记 04 类和对象

    一.类和对象的概念 类 >>具有相同属性和行为的一类实体 对象 >>实物存在的实体.通常会将对象划分为两个部分,即静态部分和动态部分.静态部分指的是不能动的部分,被称为属性,任 ...

  8. Java学习笔记-File类的基本方法

    要渐渐养成写博客的习惯-----> 前段时间看Mars的java中的I/O流没怎么懂,发现I/O流好难啊.今天重新看一遍其他教学,还有书籍,做些笔记,记录下每天的学习生活. File类的一些方法 ...

  9. Java学习笔记-嵌套类

    嵌套类 嵌套类有两种类别:static and non-static,分别对应为静态嵌套类和内部类. class OuterClass { ... static class StaticNestedC ...

随机推荐

  1. Java数据结构和算法(十)——二叉树

    接下来我们将会介绍另外一种数据结构——树.二叉树是树这种数据结构的一员,后面我们还会介绍红黑树,2-3-4树等数据结构.那么为什么要使用树?它有什么优点? 前面我们介绍数组的数据结构,我们知道对于有序 ...

  2. Maven构建真正的J2EE项目

    今天同事问起我眼下用Maven构建的多模块项目架构和曾经用Eclipse创建的Web项目的问题.以下将讲一下使用maven搭建多模块的J2ee项目,以及採用这样的方式搭建项目对日后项目的水平拆分和垂直 ...

  3. Esri:为Web GIS注入新内涵

    纵观近些年IT与空间技术的发展,云计算.大数据.实时信息.LBS.无人机.倾斜摄影等新技术层出不穷:互联网基础设施建设成绩瞩目,宽带成为国家战略性公共基础设施. GIS(地理信息系统)作为空间信息分析 ...

  4. Java的单例模式

    单例模式:单例模式确保其一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式又分为:懒汉式,饿汉式等; 特点: a.单例只有一个实例. b.必须自己创建自己唯一的实例 c.单例类必须 ...

  5. 微信小程序的Web API接口设计及常见接口实现

    微信小程序给我们提供了一个很好的开发平台,可以用于展现各种数据和实现丰富的功能,通过小程序的请求Web API 平台获取JSON数据后,可以在小程序界面上进行数据的动态展示.在数据的关键 一环中,我们 ...

  6. url特殊字符转义及解决方法

    URL特殊字符需转义 1.空格换成加号(+) 2.正斜杠(/)分隔目录和子目录 3.问号(?)分隔URL和查询 4.百分号(%)制定特殊字符 5.#号指定书签 6.&号分隔参数 转义字符的原因 ...

  7. (转)java内部类详解

    本文转自http://www.cnblogs.com/dolphin0520/p/3811445.html,谢谢作者 说起内部类这个词,想必很多人都不陌生,但是又会觉得不熟悉.原因是平时编写代码时可能 ...

  8. 《程序设计方法》【PDF】下载

    内容简介 <程序设计方法>主要以方法为主导,结合C语言,把程序设计方法学研究中若干成熟的理论和方法用通俗易懂的语言描述出来.<程序设计方法>还选取趣味性强.技巧性高.能够启发学 ...

  9. 【批处理】IF ERRORLEVER语句顺序注意

    @echo off dir d:\dddddd if errorlevel 1 goto 1 if errorlevel 0 goto 0 rem 两行if语句不可交换位置,否则失败了也会显示成功. ...

  10. Lucene实现索引和查询

    0引言 随着万维网的发展和大数据时代的到来,每天都有大量的数字化信息在生产.存储.传递和转化,如何从大量的信息中以一定的方式找到满足自己需求的信息,使之有序化并加以利用成为一大难题.全文检索技术是现如 ...