2

I am try to find out why below code is giving error, Can anybody explain please. Here is a class

package abc;

public class A {
   public class B {

   }
}

Now I am try to create a B class

package xyz;
import abc.*;


public class B extends A{
    public static void main(String[] args) {
        B b = new B (); // this line gives error. Can you please explain
    }
}

Please consider class B extends A is in default package means

import abc.*;


public class B extends A{
    public static void main(String[] args) {
        B b = new B (); // this line gives error. Can you please explain 
                          // I am try to create "B" class object which extends A 
                         //.. not the B inner class 
    }
}

Error shows in eclipse is : "No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A (e.g. x.new A() where x is an instance of A)."

Preetam Purbia
  • 5,736
  • 3
  • 24
  • 26
  • 1
    Please post the error you get. – Baz Aug 02 '12 at 13:41
  • And what exactly is the error? There is nothing wrong with types here, you may be trying to call a non-existent constructor. – Marko Topolnik Aug 02 '12 at 13:41
  • if you post the error, it will be something like "enclosing class instance required". See my answer for explanation. – tucuxi Aug 02 '12 at 13:46
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 04 '16 at 00:04

7 Answers7

2

Given that B extends A, and A contains an inner class B, I suspect there's room for confusion (for both compiler and programmer).

The line:

B b = new B();

is ambiguous. Which B is this ? As pointed out elsewhere, the inner class B needs an containing instance of A, so I suspect you mean the B outer class.

Edit: Your error

"No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A (e.g. x.new A() where x is an instance of A)."

confirms this. I presume you want the xyz.B and should scope that appropriately.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

Try to specify the enclosing scope:

B b = new A.B();

or

B b = new xyz.B();
Edmon
  • 4,752
  • 4
  • 32
  • 42
1

If you make public class B static, it will work as written.

Otherwise, you cannot make instances of class B from outside an instance of class A, because non-static inner classes require a pointer to their outer class's "this" (within a B, you can write this.A, and that will actually refer to the enclosing A).

tucuxi
  • 17,561
  • 2
  • 43
  • 74
0

When I create the following in Eclipse:

package com.example;

public class A {
    class B {
    }
}

and

package com.example;

import com.example.A.B;

public class B extends A {

    public static void main(String[] args) {
        B b = new B();
    }
}

As you can see Eclipse imports the class A.B . Without the import the following error is noted:

No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A (e.g. x.new A() where x is an instance of A).

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
0

The error I get when I try to compile this code (minus the package specs since they seem irrelevant) is "non-static variable this cannot be referenced from a static context". So, the problem is that within the static method main() you are trying to access something non-static, which I guess is the definition of the inner class B.

You actually get the same error if the second outer class has a different name. So the apparent naming conflict is a red herring. Although that itself is a good reason not to use the same name for both classes -- it causes confusion when trying to understand the error.

If I change the declaration of the inner class to be static, the error is resolved.

public class A {
   public static class B {

   }
}

Some relevant discussion of static vs. non-static nested classes is here.

But this may not be the correct fix. It shows that your code in main() is referencing the B inner class, not the B class that contains the method. Which did you actually want? Obviously, I'd suggest using different names for this two classes to avoid confusion. If what you want in main() is to create an instance of the class that contains that method, then using a unique name for that class will solve your problem.

Community
  • 1
  • 1
Dave Costa
  • 47,262
  • 8
  • 56
  • 72
0

Your class B extends A has inherited the class A.B and the expression new B() refers to that inner class. Therefore it asks for an enclosing instance. You must either explicitly qualify both your mentions of B with the package name, or, far better, avoid such naming mess in the first place.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

I found this solution, through reflection you can create B class(B extends A class) object.

import java.lang.reflect.Method;

import abc.*;

public class B extends A{
    public static void main(String[] args) {
        //B b = new B(); // this line gives error. Can you please explain 
                          // I am try to create "B" class object which extends A 
                         //.. not the B inner class
     try{
      Class c = Class.forName("B"); //class B extends A
      Method  m=c.getMethod("print", null);
      m.invoke(c.newInstance(), null);
     }catch(Exception e){
      e.printStackTrace();
     }
    }

    public void print(){
     System.out.println("Printed");
    }
}
Preetam Purbia
  • 5,736
  • 3
  • 24
  • 26