-1

Currently i'll try to understand extended classes in java.

What was done: I create a class and extende class for him, in extended class i create constructor with simple commands - show variables from superclass. In another file i try to create object of extended class, but i have error - "No enclosing instance of type CObj is accessible. Must qualify the allocation with an enclosing instance of type CObj (e.g. x.new A() where x is an instance of CObj)."

Code: main file

public class Demo {
    public static void main (String[] args){
        CObj.Co n=new CObj.Co();
        n.show();
    }
}

and file with classes

class CObj {
    int i,k,l;
    CObj summ (CObj object){
        object.i*=i;
        object.k*=k;
        object.l*=l;
        return object;
   }
   void show (){
       System.out.println("this is super class");
       System.out.println(i+" "+k+" "+l);
   }

... few constructors... and exntended class

class Co extends CObj{
    Co(){
        super(1,2,3);
    }
    void show(){
        System.out.println("this is extended class and overloaded meth");
        super.show();
    }
}

Question; what was done wrong? why i have this error and what i need to change? Or maybe i'm doing something in a bad way?

5
  • 5
    Define each class in its own file, until you understand what inner classes are.
    – JB Nizet
    Aug 11, 2013 at 9:50
  • 1
    It looks like you may have defined Co inside CObj. That doesn't do what you want. Put it in its own file instead. Aug 11, 2013 at 9:52
  • i have 2 files for each classes - one for superclass and extended clas and one for class with main method
    – hbk
    Aug 11, 2013 at 9:53
  • 2
    That makes 2 files for 3 classes. You should have 3 files: one for the main class, another one for the CObj class, and a third one for the Co class.
    – JB Nizet
    Aug 11, 2013 at 9:55
  • Possible duplicate of Java - No enclosing instance of type Foo is accessible
    – fabian
    Mar 4, 2016 at 0:30

3 Answers 3

4

Given your error message:

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

It says that Co is an inner class of CObj, not a nested (static inner) class. So, you can only access Co using an instance of CObj:

Cobj.Co obj = new CObj().new Co();

But, you should be really sure that you need an inner class, while creating it. Also, as @JBNizet suggests, you should put each class in it's own file. That would make it easier to for you to understand how the classes interact. And then you can create object of Co easily using:

Co obj = new Co();

Secondly, you are trying to call the super class constructor from Co, which doesn't exist:

super(1,2,3);

You don't have any constructor in CObj, which takes 3 integer parameers. That wouldnt' compile either.

1
  • In fact, given the error message, I suspect that it actually is an inner class, but shouldn't be.
    – JB Nizet
    Aug 11, 2013 at 9:51
2

Your syntax is wrong here:

CObj.Co n=new CObj.Co();

you can just write:

Co obj = new Co();

Also, you don'have an (int, int, int) constructor in base class CObj, so this line in class Co is wrong:

super(1,2,3);

Whether you change the call to the existing CObj(CObj object) or you write a CObj(int a, int b, int c)

2

Here seems to be one of your problem:

  Co(){
        super(1,2,3);
    }

You don't have such a constructor in your superclass.

3
  • i have constructors: just remove them from post: all constructors that i have below:` CObj() { i=0; k=0; l=0; } CObj(int x) { i=x; k=0; l=0; } CObj(int x, int y) { i=x; k=y; l=0; } CObj(int x, int y, int z) { i=x; k=y; l=z; } CObj (CObj object){ i=object.i; k=object.k; l=object.l; }`
    – hbk
    Aug 11, 2013 at 9:51
  • @kirill Removing unnessisary code is always a good thing but try to make sure the code is self complete Aug 11, 2013 at 10:27
  • ok, next time i'll put all required code for better understandung of current problem, thanks
    – hbk
    Aug 11, 2013 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.