Click here to Skip to main content
14,836,552 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


Java
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();
}

}
Posted
Updated 12-Nov-15 20:46pm
v2
Comments
Mohibur Rashid 13-Nov-15 4:48am
   
Your code works fine. What is your actual problem?

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
   
v3
By qualifying the outer class you gain access to its static members, e.g.
Java
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();
  }
}
   
v2
Comments
Member 11944583 13-Nov-15 7:48am
   
simple explanation.nice
CPallini 13-Nov-15 8:17am
   
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900