A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 高鑫 中级黑马   /  2012-4-29 23:13  /  2394 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class Person
  2. {
  3.         String name;
  4.         static String country="CN";
  5.         public static void show()
  6.         {
  7.                 System.out.println("name="+name+","+"country="+country);
  8.         }

  9. }
  10. class StaticDemo
  11. {
  12.         public static void main(String[] args)
  13.         {   Person p=new Person();
  14.             p.show();
  15.                 System.out.println(Person.country);
  16.         }

  17. }
  18.        
复制代码
代码中的p.show()为什么无法引用name,这里的对象已经生成了啊,即使name没有值也应该显示null啊?为什?哪的问题?

5 个回复

倒序浏览
class Person

{

        String name;//非静态变量不能被静态方法访问        
static String country="CN";

        public static void show()//静态方法不能访问非静态变量        {

                System.out.println("name="+name+","+"country="+country);

        }



}

class StaticDemo

{

        public static void main(String[] args)

        {   Person p=new Person();

            p.show();

                System.out.println(Person.country);

        }



}

评分

参与人数 1技术分 +1 收起 理由
贠(yun)靖 + 1

查看全部评分

回复 使用道具 举报
静态方法不能访问非静态变量,而非静态方法既可以访问静态变量又可以访问非静态变量。
class Person

{

        String name;//把此处的代码改为static String name;这样就可以了
        static String country="CN";

        public static void show()

        {

                System.out.println("name="+name+","+"country="+country);

        }



}

class StaticDemo

{

        public static void main(String[] args)

        {   Person p=new Person();

            p.show();

                System.out.println(Person.country);

        }



}

评分

参与人数 1技术分 +1 收起 理由
贠(yun)靖 + 1

查看全部评分

回复 使用道具 举报
  1. class Person
  2. {
  3. String name;
  4. static String country="CN";
  5. public static void show()
  6. {
  7. System.out.println("name="+name+","+"country="+country);
  8. }

  9. }
  10. class StaticDemo
  11. {
  12. public static void main(String[] args)
  13. { Person p=new Person();
  14.    p.show();
  15. System.out.println(Person.country);
  16. }

  17. }
复制代码
上面两位兄弟已经说明了错误的地方及改正方法了。但也只是针对程序本身,从理论实际上说不应该static修饰,因为name是特有的,因为每个人的名字是不同的。
但我感觉针对你的这个程序你应该知道上面的语句String name;前加static修饰后就没问题了。你
的迷惑之处在于为什么要加static修饰,你认为你已经建立了Person对象,调用应该可以的。从表面上看你是想建立Person对象后,属性name进堆,初始化为null,country进方法区赋值CN,然后用对象调用show()方法,你认为应该输出name为null。(个人揣测的)
原因就在于编译检查语法错误,show()方法是静态的,不能访问非静态的成员,在编译时期就已经出现问题。即便你的主函数里没有
任何语句编译还是会失败的。

评分

参与人数 1技术分 +1 收起 理由
贠(yun)靖 + 1

查看全部评分

回复 使用道具 举报
对啊,你的show方法是静态的,是不能访问非静态的name的
回复 使用道具 举报
01.class Person

02.{

03.        String name;//没有用static修饰,非静态变量不能被静态方法访问 ,原因是在内存中静态方法优先于对象加载,也就是说static void show()在内存中存在的时候,还没有p这个对象,那怎么能调用p对象中的name成员变量呢?如果改成static修饰,那么name成员变量是所以Person类实例对象的共享数据。它和static void show()是随着类加载而加载在内存中的,所以可以被show方法调用。

04.        static String country="CN";

05.        public static void show()

06.        {

07.                System.out.println("name="+name+","+"country="+country);

08.        }

09.

10.}

11.class StaticDemo

12.{

13.        public static void main(String[] args)

14.        {   Person p=new Person();

15.            p.show();

16.                System.out.println(Person.country);

17.        }

18.

19.}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马