• Register
1 vote
14 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

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

1 vote
1 answer 34 views
34 views
Problem : While binding the dropdown in MVC, I often face following error: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key country. Please find below my View for your reference : @Html.DropDownList("country", (IEnumerable<SelectListItem>)ViewBag ... .countryid.ToString() }); } ViewBag.mycountrydrop = myitem8; return View(); I do not know how to resolve above error.
asked Dec 28, 2019 alecxe 7.5k points
0 votes
1 answer 1.5K views
1.5K views
Problem : I have the two DataFrames which I would want to merge. I have referred many documents and also tried to perform many operations but I am not sure what to do now. Please find my two DataFrames as below: DataFrame1: id name type currency 0 BTTA.S Apple ... here I met with the exception as below : ValueError: can not merge DataFrame with instance of type <class 'pandas.core.series.Series'>
asked Dec 24, 2019 alecxe 7.5k points
0 votes
1 answer 87 views
87 views
Problem: I am totally new to C programming, but I must code this English - French text translator by next week. It should replace the words with a given word or phrases.My texts are the following "Hello", "What" and "How are you?" and I am trying to ... the argument types testtranslator C2664 'size_t strlen(const char *)': cannot convert argument 1 from 'const char *[2]' to 'const char *'
asked Aug 29 Raphael Pacheco 4.9k points
0 votes
1 answer 5 views
5 views
let's imagine we have two classes A and B: from class A in private I create a vector of pointers to instances of B vector <B *> punt; How would you add pointers to instances of B in that vector? How would those pointers be removed from the vector? Like a normal ... (name); // assuming you have a constructor that assigns the name     punt.push_back (newB); ??? Thank you very much and greetings !
asked Aug 14 game 4.6k points