No enclosing instance of type Foo is accessible
I have the following code:
class Hello {
class Thing {
public int size;
Thing() {
size = 0;
}
}
public static void main(String[] args) {
Thing thing1 = new Thing();
System.out.println("Hello, World!");
}
}
I know Thing
does nothing, but my Hello, World program compiles just fine without it. It's only my defined classes that are failing on me.
And it refuses to compile. I get No enclosing instance of type Hello is accessible."
at the line that creates a new Thing. I'm guessing either:
Any ideas?
static class Thing
will make your program work.
As it is, you've got Thing
as an inner class, which (by definition) is associated with a particular instance of Hello
(even if it never uses or refers to it), which means it's an error to say new Thing();
without having a particular Hello
instance in scope.
If you declare it as a static class instead, then it's a "nested" class, which doesn't need a particular Hello
instance.
You've declared the class Thing as a non-static inner class. That means it must be associated with an instance of the Hello class.
In your code, you're trying to create an instance of Thing from a static context. That is what the compiler is complaining about.
There are a few possible solutions. Which solution to use depends on what you want to achieve.
Change Thing to be a static nested class.
static class Thing
Create an instance of Hello, then create an instance of Thing.
public static void main(String[] args)
{
Hello h = new Hello();
Thing thing1 = h.new Thing(); // hope this syntax is right, typing on the fly :P
}
Move Thing out of the Hello class.
For more information on nested/inner classes: Nested Classes (The Java Tutorials)
Well... so many good answers but i wanna to add more on it. A brief look on Inner class in Java- Java allows us to define a class within another class and Being able to nest classes in this way has certain advantages:
It can hide(It increases encapsulation) the class from other classes - especially relevant if the class is only being used by the class it is contained within. In this case there is no need for the outside world to know about it.
It can make code more maintainable as the classes are logically grouped together around where they are needed.
The inner class has access to the instance variables and methods of its containing class.
We have mainly three types of Inner Classes
Some of the important points to be remember
Let`s try to see the above concepts practically_
public class MyInnerClass {
public static void main(String args[]) throws InterruptedException {
// direct access to inner class method
new MyInnerClass.StaticInnerClass().staticInnerClassMethod();
// static inner class reference object
StaticInnerClass staticInnerclass = new StaticInnerClass();
staticInnerclass.staticInnerClassMethod();
// access local inner class
LocalInnerClass localInnerClass = new MyInnerClass().new LocalInnerClass();
localInnerClass.localInnerClassMethod();
/*
* Pay attention to the opening curly braces and the fact that there's a
* semicolon at the very end, once the anonymous class is created:
*/
/*
AnonymousClass anonymousClass = new AnonymousClass() {
// your code goes here...
};*/
}
// static inner class
static class StaticInnerClass {
public void staticInnerClassMethod() {
System.out.println("Hay... from Static Inner class!");
}
}
// local inner class
class LocalInnerClass {
public void localInnerClassMethod() {
System.out.println("Hay... from local Inner class!");
}
}
}
I hope this will helps to everyone. Please refer for more
链接地址: http://www.djcxy.com/p/88886.html上一篇: jLabels自动换行
下一篇: 没有可以访问Foo类型的封闭实例