Anand
Anand

Reputation: 23

The allocation without an enclosing instance raised an exception

I am getting this error :

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

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

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


at casting.PrimitiveCasting.main(PrimitiveCasting.java:22)

package casting;

public class PrimitiveCasting {
    class anand {
        int a = 90;

        void anand1() {
            System.out.println("anand is having anand1");
        }
    }

    class babu extends anand {
        int c, b = 88;

        void babu1() {
            System.out.println("babu is having babu1");
        }
    }

    public static void main(String[] args) {
        System.out.println("**********");
         anand z1= new anand();
         z1.anand1();

         babu b1= new babu();
         b1.anand1();
         b1.babu1();`enter code here`
         System.out.println("********");
    }

}

Upvotes: 1

Views: 128

Answers (4)

Zava
Zava

Reputation: 400

This is normal, in your code you define your anand class to be tied with instances of PrimitiveClass, ie, you can have a instance of anand class from an instance of PrimitiveClass. That's what we call a inner class. For your code to work you can mark you anand (and babu1) class to be static. Here is the code :

public class PrimitiveCasting {
static class anand {
    int a = 90;

    void anand1() {
        System.out.println("anand is having anand1");
    }
}

static class babu extends anand {
    int c, b = 88;

    void babu1() {
        System.out.println("babu is having babu1");
    }
}

public static void main(String[] args) {
    System.out.println("**********");
     anand z1= new anand();
     z1.anand1();

     babu b1= new babu();
     b1.anand1();
     b1.babu1();
     System.out.println("********");
}

}

Or you can make an instance of PrimitiveClass and then instantiate anand or babu, like this :

public class PrimitiveCasting {
class anand {
    int a = 90;

    void anand1() {
        System.out.println("anand is having anand1");
    }
}

class babu extends anand {
    int c, b = 88;

    void babu1() {
        System.out.println("babu is having babu1");
    }
}

public static void main(String[] args) {
    System.out.println("**********");
    PrimitiveCasting primitiveCasting = new PrimitiveCasting();
    anand z1= primitiveCasting.new anand();
    z1.anand1();

    babu b1= primitiveCasting.new babu();
    b1.anand1();
    b1.babu1();
    System.out.println("********");
}

}

Hope this help!

Upvotes: 1

yuan
yuan

Reputation: 2534

The Java programming language allows you to define a class within another class. Such a class is called a nested class
Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes.
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Upvotes: 0

Panther
Panther

Reputation: 3339

You are getting error as your inner classes are not static member variable , so cannot be access from static method main. Either make those inner classes static like :-

public class PrimitiveCasting  {
static class anand {
    int a = 90;

    void anand1() {
        System.out.println("anand is having anand1");
    }
}

Or create instance of PrimitiveCasting in main method to access those classes :-

PrimitiveCasting pc = new PrimitiveCasting();

     anand z1= pc.new anand();
     z1.anand1();

Upvotes: 0

Bhargav Kumar R
Bhargav Kumar R

Reputation: 2200

You should create instance of anand this way.

PrimitiveCasting pc = new PrimitiveCasting();
anand z1 = pc.new anand();

The reason being since your are referring anand from static context it should be qualified with enclosing instance type.

Same is the case for other class as well.

Upvotes: 0

Related Questions