在Java中同类中的反射调用静态方法法能否调用类成员变量呢?为什么?

&(注:静态变量修改为静态成员变量,静态方法改为静态成员方法)
静态成员变量又称类变量,静态成员方法又称类方法,它们统称为静态成员或类成员。静态成员由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");
if(getGender() == "male")
System.out.println("I am a boy");
System.out.println("Something is wrong!");
System.out.println("My hobby is " + hobby);
if(citizenship == "Chinese")
System.out.println("I am a chinese");
if(citizenship == "US")
System.out.println("I am an American");
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如下:
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类:
public class Person {
protected S
static String citizenship = "Chinese";
public Person() {
public Person(String n, String g) {
this.name =
this.gender =
public Person(String n, int a, String g, String h) {
this.name =
this.age =
this.gender =
this.hobby =
//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 =
this.age =
this.gender =
this.hobby =
this.residence =
public void setName(String n) {
this.name =
public void setAge(int a) {
this.age =
public void setGender(String g) {
this.gender =
public void setHobby(String h) {
this.hobby =
public void setResidence(String r) {
this.residence =
public String getName() {
return this.
public int getAge() {
return this.
public String getGender() {
return this.
public String getHobby() {
return this.
public String getResidence() {
return this.
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");
if(getGender() == "male")
System.out.println("I am a boy");
System.out.println("Something is wrong!");
System.out.println("My hobby is " + hobby);
if(citizenship == "Chinese")
System.out.println("I am a chinese");
if(citizenship == "US")
System.out.println("I am an American");
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();
阅读(...) 评论()trackbacks-0
前言:这属于 java 的基础知识,你可以不了解,也可以不拿它当一回事,代码也一样能敲的顺畅;这是很久之前,我从 C++ 转 java 刚入门那会的看法,那会我坚持自己的理念:&&&&重实践,讲运用,轻理论。当然,对同一样事物,在不同阶段也会有不一样的看法,这理念在某个时段已经不适合我,早就被抛弃了。不扯淡了,直接进入主题。java 中的变量大致分为 成员变量 和 局部变量 两大类。成员变量:&&&&在类体里面定义的变量称为成员变量;&&&&如果该成员变量有 static 关键字修饰,则该成员变量称为 静态变量 或 类变量;&&&&如果该成员变量没有 static 关键字修饰,则该成员变量被称为 非静态变量 或 实例变量。局部变量:&&&&形参、方法内定义的变量、代码块中定义的变量,都属于局部变量。类变量 (静态变量)&&&&1. 可以向前引用&&&&2. 变量属于类本身&&&&3. 类变量不依赖类的实例,类变量只在初始化时候在栈内存中被分配一次空间,无论类的实例被创建几次,都不再为类变量分配空间&&&&4. 通过类的任意一个实例来访问类变量,底层都将将其转为通过类本身来访问类变量,它们的效果是一样的&&&&5. 一旦类变量的值被改变,通过类或类的任意一个实例来访问类变量,得到的都将是被改变后的值&&&&6. 将在类的初始化之前初始化实例变量(非静态变量)&&&&1. 不能向前引用,如果向前引用,则称为非法向前引用,这是不允许的&&&&2. 变量属于类的实例对象&&&&3. 随着类的实例被创建而分配内存空间非静态代码块&&&&直接由 { } 包起来的代码,称为非静态代码块静态代码块&&&&直接由 static {&} 包起来的代码,称为静态代码块类变量(静态变量)、实例变量(非静态变量)、静态代码块、非静态代码块 的初始化时机&&&&由 static 关键字修饰的(如:类变量[静态变量]、静态代码块)将在类被初始化创建实例对象之前被初始化,而且是按顺序从上到下依次被执行;&&&&没有 static 关键字修饰的(如:实例变量[非静态变量]、非静态代码块)初始化实际上是会被提取到类的构造器中被执行的,但是会比类构造器中的&&&&代码块优先执行到,其也是按顺序从上到下依次被执行。- 以上是本人在翻完 PDF 后的个人笔记和理解以及见解,不见得百分百对,以下附上本人测试示例代码,会更有说服性示例代码
&1&2/**&3&*&-----------------------------------------&4&*&&#64;文件:&Statical.java&5&*&&#64;作者:&fancy&6&*&&#64;邮箱:&fancydeepin&#64;yeah.net&7&*&&#64;时间:&&8&*&&#64;描述:&TEST&9&*&-----------------------------------------<span style="color: #&*/<span style="color: #public&class&Statical&{<span style="color: #<span style="color: #&/**<span style="color: #&&&&&*&静态代码块<span style="color: #&&&&&*&类变量(静态变量)可以向前引用(即:先引用,再定义)<span style="color: #&&&&&*/<span style="color: #&&&&static&{<span style="color: #&&&&&&&&name&=&"fancydeepin";&//&name&的定义在使用之后<span style="color: #&&&&&&&&System.out.println("---&&&静态代码块被执行&&&&---");<span style="color: #&&&&}<span style="color: #&/**<span style="color: #&&&&&*&类变量(静态变量)在类的初始化之前初始化,无论类的实例将被创建多少个<span style="color: #&&&&&*&类变量(静态变量)都将只在初始化时候在栈内存上分配一次空间<span style="color: #&&&&&*&凡&static&修饰的,都将按位置被顺序执行,所以,<span style="color: #&&&&&*&name&的值最终输出&fancy&而不是上面的&fancydeepin<span style="color: #&&&&&*/<span style="color: #&&&&public&static&String&name&=&"fancy";&//类变量(静态变量)<span style="color: #&&&&private&String&mail&=&"myEmail";&//实例变量(非静态变量),定义时指定初始值,会比在构造器赋予值更早执行<span style="color: #&&&&<span style="color: #&&&&public&Statical() {<span style="color: #&&&&&&&&mail&=&"fancydeepin&#64;yeah.net";<span style="color: #&&&&&&&&System.out.println("---&&构造器代码块被执行&&---");<span style="color: #&&&&}<span style="color: #&/**<span style="color: #&&&&&*&非静态代码块<span style="color: #&&&&&*&实际上,非静态代码块在类初始化创建实例时,将会被提取到类的构造器中,<span style="color: #&&&&&*&但是,非静态代码块会比构造器中的代码块优先被执行<span style="color: #&&&&&*&所以,mail&最终输出的是类构造器中给定的值,也就是&fancydeepin&#64;yeah.net<span style="color: #&&&&&*&而不是&&#64;qq.com,更不是&myEmail<span style="color: #&&&&&*/<span style="color: #&&&&{<span style="color: #&&&&&&&&mail&=&"<span style="color: #;qq.com";<span style="color: #&&&&&&&&System.out.println("---&&非静态代码块被执行&&---");<span style="color: #&&&&}<span style="color: #&&&&<span style="color: #&&&&//&getting&and&setting&<span style="color: #
测试类示例代码
&1&2&&&&& /**&3&&&&&&&&&*&类变量(静态变量)在类被初始化创建实例之前被初始化&4&&&&&&&&&*/&5&&&&&&&&System.out.println("-----------------&&&#64;1&&----------------");&6&&&&&&&&System.out.println("name&---&&&"&+&Statical.name);&//&&#64;1&7&&&&&&&&System.out.println("-----------------&&&#64;1&&----------------");&8&&&&&&&/**&9&&&&&&&&&*&创建类的实例对象<span style="color: #&&&&&&&&&*/<span style="color: #&&&&&&&&System.out.println("-----------------&&&#64;6&&----------------");<span style="color: #&&&&&&&&Statical&statical&=&new&Statical();&//&&#64;6<span style="color: #&&&&&&&&System.out.println("-----------------&&&#64;6&&----------------");<span style="color: #&&&&&&& /**<span style="color: #&&&&&&&&&*&通过实例来访问类变量,底层将转化成通过类本身来访问类变量<span style="color: #&&&&&&&&&*/<span style="color: #&&&&&&&&System.out.println("-----------------&&&#64;2&&----------------");<span style="color: #&&&&&&&&System.out.println("name&---&&&"&+&statical.name);&//&&#64;2<span style="color: #&&&&&&&&System.out.println("-----------------&&&#64;2&&----------------");<span style="color: #&&&&&&&/**<span style="color: #&&&&&&&&&*&如果类变量的值被改变,再访问类变量,将得到被改变后的值<span style="color: #&&&&&&&&&*/<span style="color: #&&&&&&&&Statical.name&=&"fancydeepin";<span style="color: #&&&&&&&&<span style="color: #&&&&&&&&System.out.println("-----------------&&&#64;3&&----------------");<span style="color: #&&&&&&&&System.out.println("name&---&&&"&+&statical.name);&//&&#64;3<span style="color: #&&&&&&&&System.out.println("-----------------&&&#64;3&&----------------");<span style="color: #&&&&&&&&<span style="color: #&&&&&&&&System.out.println("-----------------&&&#64;4&&----------------");<span style="color: #&&&&&&&&System.out.println("name&---&&&"&+&Statical.name);&//&&#64;4<span style="color: #&&&&&&&&System.out.println("-----------------&&&#64;4&&----------------");<span style="color: #&&&&&&&/**<span style="color: #&&&&&&&&&*&非静态代码块&和&构造器&被执行的时机<span style="color: #&&&&&&&&&*/<span style="color: #&&&&&&&&System.out.println("-----------------&&&#64;5&&----------------");<span style="color: #&&&&&&&&System.out.println("mail&---&&&"&+&statical.getMail());&//&&#64;5<span style="color: #&&&&&&&&System.out.println("-----------------&&&#64;5&&----------------");<span style="color: #
后台输出结果
-----------------&&&#64;<span style="color: #&&-------------------&&&静态代码块被执行&&&&---name&---&&&fancy-----------------&&&#64;<span style="color: #&&---------------------------------&&&#64;<span style="color: #&&-------------------&&非静态代码块被执行&&------&&构造器代码块被执行&&--------------------&&&#64;<span style="color: #&&---------------------------------&&&#64;<span style="color: #&&----------------name&---&&&fancy-----------------&&&#64;<span style="color: #&&---------------------------------&&&#64;<span style="color: #&&----------------name&---&&&fancydeepin-----------------&&&#64;<span style="color: #&&---------------------------------&&&#64;<span style="color: #&&----------------name&---&&&fancydeepin-----------------&&&#64;<span style="color: #&&---------------------------------&&&#64;<span style="color: #&&----------------mail& ---&&&fancydeepin&#64;yeah.net-----------------&&&#64;<span style="color: #&&----------------
为了能更好的看出后台的输出是哪一行代码执行的结果,我用了 &#64;和数字来做了标记,希望大家不要看晕了哈 ^_^最后针对输出结果简单说一下:&#64;1 说明,静态(类变量、静态代码块)属于类本身,不依赖于类的实例&#64;6 说明,在创建类的实例对象的时候,非静态代码块比构造器代码块更早的执行&#64;3 4&说明,当类变量的值改变后,再通过类或类的实例来访问类变量,得到的将是被改变后的值&#64;5 说明,非静态(实例变量、非静态代码块)的地位是相等的,它们将按顺序被执行,但会比构造器中的代码块更早的执行 &&
阅读(11097)
&re: java 中的 成员变量、局部变量、静态变量、类变量、非静态变量、实例变量、向前引用、非法向前引用、静态代码块、非静态代码块 执行时机[未登录]
类变量只被加载到方法区,而不是栈内存&&&&&&
&re: java 中的 成员变量、局部变量、静态变量、类变量、非静态变量、实例变量、向前引用、非法向前引用、静态代码块、非静态代码块 执行时机
请问一个问题类的成员变量,不能够在类中使用 这是为什么呢比如class Test{
private List list = new ArrayList();
list.add(&abc&);// 在这里是不能使用 list变量的 原因是什么呢?
&re: java 中的 成员变量、局部变量、静态变量、类变量、非静态变量、实例变量、向前引用、非法向前引用、静态代码块、非静态代码块 执行时机[未登录]
&#64;colorain它是需要在方法中执行的。&&&&&&
随笔分类(8)
随笔档案(104)
积分与排名
阅读排行榜他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)46被浏览21,797分享邀请回答214 条评论分享收藏感谢收起111 条评论分享收藏感谢收起}

我要回帖

更多关于 对象能否调用静态方法 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信