静态嵌套类的问题 [英] Issue on static nested class

查看:52
本文介绍了静态嵌套类的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从静态嵌套类的实例访问静态成员。



这里demoonstatic是一个外部类,而innerclass是静态嵌套类.i我无法从内部类的实例调用外部类的静态成员。请帮助我解决这个问题。





i am not able to access static members from instance of the static nested class.

Here demoonstatic is a outer class and innerclass is the static nested class.i am not able to invoke static member of outer class from instance of inner class.please help me on this issue.


public class demoonstatic {
private static int i;
    public static  void  method()
    {
        System.out.println("outer class method");
    }
    public static class innerclass
    {
            int j=0;
            static int z=10;
    }
    public static void main(String[] args) {

        System.out.println(innerclass.z);
innerclass innerclassobject=new innerclass();
}

}

推荐答案

要访问某个内部类的成员它的外部类,你需要至少内部



此外,任何静态的所有成员class必须是静态的。



-SA
To access a member of some inner class by a member of its outer class, you will need to make it at least internal.

Also, all members of any static class have to be static.

—SA


通过限定外部类,您获得访问权限其静态成员,例如

By qualifying the outer class you gain access to its static members, e.g.
public class Outer
{
  public static void hi()
  {
    System.out.printf("hi from Outer\n");
  }

  public static class Inner
  {
    public static void hello()
    {
      System.out.printf("hello from Inner and ");
      Outer.hi(); // access to Outer class
    }
  }

  public static void main(String [] arg)
  {
    Inner.hello();
  }
}


这篇关于静态嵌套类的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆