0

Possible Duplicate:
Why cant we have static method in an inner class?

I don't know why an inner class cannot have a static method. Why is it wrong?

class A1
{
    class B1
    {
         static void fun()
        {
            System.out.println("HIII");
        }
    }
    public static void main(String[] args)
    {
        A1 a = new A1();
        A1.B1 b = new a.B1();
        b.fun();
    }
}

If an outer class object can access a static method and a variable y not a inner class get the access? What are the problems.

Community
  • 1
  • 1
sai sindhu
  • 1,155
  • 5
  • 20
  • 30
  • 2
    dupplicate : http://stackoverflow.com/questions/975134/why-cant-we-have-static-method-in-an-inner-class –  May 20 '12 at 10:16
  • 1
    [duplicate](http://stackoverflow.com/q/975134/645270) – keyser May 20 '12 at 10:16

1 Answers1

1

The problem here is that your non-static inner class has a static method and you are trying to call it from static method of outer class.

Non-static inner class is "relevant" only in context of instance of outer class because it can access outer class' nont-static methods and to this of outer class (using A1.this).

Bottom line: if you want to do this mark inner class as static too:

static class B1
{
     static void fun()
    {
        System.out.println("HIII");
    }
}

Now you can call its static methods from outer class's static method.

AlexR
  • 114,158
  • 16
  • 130
  • 208