• Register
1 vote
67 views

Problem:

Hi

I was just learning and practicing java inner class and instance. Here I created a demo inner class though it does nothing in my program I know that. Without the inner class my code compiles well and prints Hello World. When I add the inner class it throws back the error again.
Any solution?

class Hello {
    class Demo {
        public int size;

        Demo() {
            size = 0;
        }
    }

    public static void main(String[] args) {
        Demo demo1 = new Demo();
        System.out.println("Hello, World!");
    }
}

 

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution:

The no enclosing instance of type is accessible is a compilation based error. It occurs when you are trying to initialize the instance of an inner class without specifying the instance of the outer class. Inner classes are not accessible directly using the Outer class name as only static inner classes are accessible that way.
 

Check below programs

public class OuterClass {

     public class InnerClass {
     }
} 

OuterClass.InnerClass = new OuterClass.InnerClass(); // This line gives error

To avoid this error either make the inner class as a static inner class or Initialize the Inner class using the Outer class instance.

As a static class (Inner class):

public class OuterClass {

     public static class InnerClass {
     }
} 

OuterClass.InnerClass = new OuterClass.InnerClass(); 


Initializing the inner class:

public class OuterClass {

     public class InnerClass {
     }
} 

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

This should work fine now.

13 9 6
94,240 points

Related questions

0 votes
1 answer 14 views
14 views
must qualify the allocation with an enclosing instance of type
asked Feb 16 ArifulIslam 7.5k points
1 vote
1 answer 1 view
1 view
Problem: Can anyone tell me the solution of this problem? member names cannot be the same as their enclosing type
asked 6 days ago MUHAMMAD MUNEEB 80k points
0 votes
1 answer 1 view
1 view
Problem: Could anyone please help me resolve this uncaught error? Is not accessible the request could not be performed because of an i/o device error.
asked Apr 1 rakib1 51.3k points
0 votes
1 answer 2 views
2 views
Problem: When I start up an Experimental instance of VS from VS for debugging and stop debugging (sometimes directly from the parent VS), a zombie devenv.exe process remains running which I am unable to kill. It holds on to many of my dlls Any ideas on how to kill such a process on Windows?
asked Mar 19 ummesalma 22.9k points
0 votes
1 answer 1.1K views
1.1K views
Problem : I get the below error in my browser template might not exist or might not be accessible by any of the configured template resolvers
asked Nov 13, 2019 peterlaw 6.9k points
0 votes
1 answer 9 views
9 views
Problem: Removed file is still accessible by collaborators in Google Drive. How I can delete it completely?
asked Apr 2 Sana8989 8.5k points
1 vote
1 answer 834 views
834 views
Problem : I have recently started using the Google drive. If I try to delete the file from the Google drive which was shared by somebody with me some days back then it showed me the message as shown below for your reference: "Google drive one removed file is still ... was actually deleted from my drive but problem is it will be shown in the shared with me. How can I delete the file completely?
asked May 6, 2020 stewart 4k points
0 votes
1 answer 8 views
8 views
Problem: I work on a small Django app and get an error tell me: Super(type, obj): obj must be an instance or subtype of type Can someone explain the reason for the error to me and how to solve it?
asked Mar 22 ummesalma 22.9k points
0 votes
1 answer 20 views
20 views
Problem: HELP! I need help understanding this error: Can not merge dataframe with instance of type <class 'pandas.core.series.series'>.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 9 views
9 views
Problem: I am getting this valueerror: can not merge dataframe with instance of type <class 'pandas.core.series.series'>. Kindly Help..!
asked Mar 6 Umar Ahmad 4.1k points