What causes error No enclosing instance of type Foo is accessible and how do I fix it?
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.
Error: No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() wh
SOLUTION 1
Separating the class Room from the class Main did the trick for me:
File Main.java:
public class Main {
public static void main(String[] args) {
Room office = new Room(17, 0, 10);
Room lecture = new Room(2, 0, 10);
Room lab = new Room(18, 1, 1);
System.out.println(office); // => "17-0.10"
System.out.println(lecture); // => " 2-0.10"
System.out.println(lab); // => "18-1.01"
}
}
File Room.java:
public class Room {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + etage;
result = prime * result + gebäude;
result = prime * result + raumnummer;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Room other = (Room) obj;
if (etage != other.etage)
return false;
if (gebäude != other.gebäude)
return false;
if (raumnummer != other.raumnummer)
return false;
return true;
}
public int gebäude;
public int etage;
public int raumnummer;
public Room(int gebäude, int etage, int raumnummer) {
super();
this.gebäude = gebäude;
this.etage = etage;
this.raumnummer = raumnummer;
}
@Override
public String toString() {
String s = String.format("%2s-%s.%02d", this.gebäude, this.etage, this.raumnummer);
return s;
}
}
Let me investigate why this happens and I will post the reason. By now, this is a suitable fix if you don't need both classes to be on the same file.
SOLUTION 2
Make room static:
...
class Main {
public static class Room {
@Override
public int hashCode() {
final int prime = 31;
...
WHY THIS HAPPENS
As stated in this post, by having Room as an inner class of Main, you are forcing the instances of Room to have an instance o Main. When using the operator new on the inner class Room without making a new instance of Main an error is produced.
By making the class Room static the class doesn't need an instance of Main.
No enclosing instance is accessible. Must qualify the allocation with an enclosing instance of type (e.g. x.new A() where x is an instance of )
You declared you SimpleCircle class as inner class for TestSimpleCircle.
You need either move it into a separate file or declare it as
static class SimpleCircle
Getting error: No enclosing instance of type **** is accessible
The problem is that is missing a right curly brace at Project2 class and rest a right curly brace at Superhero2 class.
Add a right curly brace at Project2 class:
//Catwoman uses thief
catwoman.setThief(true);
if(catwoman.getThief()){
System.out.println("Catwoman is easily able to sneak at night due to her master theif skills");
}
}
} //Add this right curly brace to close the Project2 class
Remove the last right curly brace from Superhero2 class:
}//End Superhero2 -> Remove this right curly brace
I recommend you always indent your code correctly, thus you can avoid this kind of issue.
No enclosing instance of type MainRender is accessible
The issue is that the Handler is a non-static nested class of MainReader. This means that you need an instance of MainReader to be able to instantiate the Handler. Have a look at this stackoverflow answer for more information about non-static vs static nested classes.
To solve the above issue, either you can make the Handler class static (if you can) or replace
Handler h = new Handler();
with
Handler h = r.new Handler();
No enclosing instance of type ... is accessible
Your nested class requires an instance of the outer class, because it's not static - but you don't have an instance of the outer class.
Try making both of your nested classes static
. It doesn't look like they need a reference to the outer class anyway.
In fact, I'd be tempted to avoid using nested classes at all for this - while nested classes can certainly be useful sometimes, they have various corner cases, and it's typically cleaner to create separate top-level types.
Generic warning - No enclosing instance of type
It happens because you created inner class Course
into PR3
and insertCoursesToSet
method is static
.
If you interesting to leave it as inner class, make it static
also:
public static class Course<E>{
private E idOrName;
private float avg;
public Course(E idOrName, float avg){
this.idOrName = idOrName;
this.avg = avg;
}
}
OR
Make insertCoursesToSet
non-static and you can write:
private void insertCoursesToSet(Set<Course> set, int n, int it) {
Scanner s = new Scanner(System.in);
if(it==1)
for(int i=0 ; i<n ; i++){
System.out.println("Please enter name:");
s.nextLine(); //clear buffer
String name = s.nextLine();
System.out.println("Please enter avg:");
float avg = s.nextFloat();
set.add(new Course<String>(name,avg));
}
else
for(int i=0 ; i<n ; i++){
System.out.println("Please enter id:");
Integer id = s.nextInt();
System.out.println("Please enter avg:");
float avg = s.nextFloat();
set.add(new Course<Integer>(id,avg));
}
}
public class Course<E>{
private E idOrName;
private float avg;
public Course(E idOrName, float avg){
this.idOrName = idOrName;
this.avg = avg;
}
}
No enclosing instance of type foo is accessible. Why?
Your classes Chicken
and Apple
are Inner Classes and the way to access any Inner Class's function is OuterClass.InnerClass innerObject = outerObject.new InnerClass()
.
You have created the object of Outer Class here: InterfaceTestClass x = new InterfaceTestClass();
and thus when you put x.new Chicken()
you are facilitating the use of Inner Class function.
Look up Inner Classes to understand the concept better. I hope this helped.
No enclosing instance of type is accessible. Error and can any body help me ive been trying changing everything but no result
As hilo
is an inner class
of programa4
there must be an isnstance of programma4
first before you can create an inner for it.
Making it static class hilo
breaks that requirement.
static class hilo extends Thread {
The alternative would be to create a programa4
to attach it to.
new programa4().new hilo(z).start();
Related Topics
Is Java "Pass-By-Reference" or "Pass-By-Value"
The Use of Multiple Jframes: Good or Bad Practice
Should I Avoid the Use of Set(Preferred|Maximum|Minimum)Size Methods in Java Swing
Manipulating an Access Database from Java Without Odbc
Including All the Jars in a Directory Within the Java Classpath
Cannot Make a Static Reference to the Non-Static Method
Connect Java to a MySQL Database
How to Round a Number to N Decimal Places in Java
"Implements Runnable" VS "Extends Thread" in Java
Assigning Variables With Dynamic Names in Java
How to Create a Java String from the Contents of a File
Difference Between == and Equals() in Java
How to Launch an Activity from Another Application in Android