-4
public class EnclosingClass{


    public void enclosingClassMethod(){
          InnerClass iC = new InnerClass();
          ic.innerClassPrivateMethod();   // this one works but why the following line doesn't       //work

          InnerClass.innerClassPrivateMethod();  // Why I can't call the method like this?


    }



    public class InnerClass{
         private void innerClassPrivateMethod(){

         }

    }

}

Why we can't call the innerClassPrivateMethod() like InnerClass.innerClassPrivateMethod()? I see "Cannot make a static reference to the non-static method innerClassPrivateMethod() from the type EnclosingClass"

If I change both the innerClassPrivateMethod and enclosingClassMethod as private I get error for

 InnerClass iC = new InnerClass();

as "No enclosing instance of type Basics6 is accessible. Must qualify the allocation with an enclosing instance of type enclosingClass."

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Siva
  • 209
  • 3
  • 11
  • To answer the title question: you definitely can. What error does your code throw? Have you looked it up? – Sotirios Delimanolis May 27 '14 at 00:32
  • 3
    because it isn't static. You need an object to access it. – nook May 27 '14 at 00:33
  • It looks like you're trying to call it statically when its not static – Vince May 27 '14 at 00:34
  • 1
    `I am not getting any error.` Then obviously there is no issue...Please be clear in what you are asking. – Sotirios Delimanolis May 27 '14 at 00:34
  • 1
    Sorry, I am getting Cannot make a static reference to the non-static method innerClassPrivateMethod() error. But here everything is non-static, right? I haven't used static keyword – Siva May 27 '14 at 00:38
  • Please take that error message and put it into SO's search tool. – Sotirios Delimanolis May 27 '14 at 00:39
  • @Sotirios Delimanolis Apart from that error I am also facing the new issue, where if I change methods to private, I get the following error. No enclosing instance of type Basics6 is accessible. Must qualify the allocation with an enclosing instance of type enclosingClass. – Siva May 27 '14 at 00:42
  • 1
    Ok, now take that error message and put it into SO's search tool. – Sotirios Delimanolis May 27 '14 at 00:43
  • The `innerClassPrivateMethod` is an instance method, so it is not applicable to the class itself. That is why you can't call it from the `InnerClass` type directly. – Will Marcouiller May 27 '14 at 00:49
  • possible duplicate of [calling non-static method in static method in Java](http://stackoverflow.com/questions/2042813/calling-non-static-method-in-static-method-in-java) – Raedwald May 27 '14 at 22:37

2 Answers2

2

In case you want to call the method of inner class using class name or we can say statically then you need to do following changes.

  1. Declare inner class method to be static.
  2. A non static inner class can never have a static method So you need to change inner class to be static.

    public class EnclosingClass{

    public void enclosingClassMethod(){
          InnerClass iC = new InnerClass();
          iC.innerClassPrivateMethod();   // this one works but why the following line doesn't       //work
    
          InnerClass.innerClassPrivateMethod();  // This also works...
    
    
    }
    
    
    
    public static class InnerClass{     //  Class converted to static 
         private static void innerClassPrivateMethod(){   // Method converted to static
    
         }
    
    }
    

    }

Sumit Tyagi
  • 431
  • 2
  • 14
1

If the method is not denoted static, it is an instance method. In this case the instance must be the inner class. Since you don't access the method through an instance in the second call, there is no this reference, etc.

Defining static methods in an inner class is quite useless. First of all is the "supporting class" of a static method arbitrary. I might add a public static void sort (int[] data); as well to a Utils class as to a ThisIsAUselessClass. The only reason why it matters is because you sometimes want to make such methods private. Now since a class can see all (inclusing private) members of its inner classes and inner classes have access to all the private methods of its "outer class", there is no reason to define a static method in an inner class.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555