No Enclosing Instance of Type Server Is Accessible

No enclosing instance of type Server is accessible

Server.ReceiveDataListener is a (non-static) inner class. You are creating it from a static context. You need to supply an instance of Server to be the outer instance. However, almost certainly you want ReceiveDataListener to be a static nested class, or probably an outer class.

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.

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

No enclosing instance of type is accessible

Try this. Removed the methods for simplicity

public class Test1 {     

public static void main( String [] args)
{
MyTriangle h1 = new MyTriangle();
}
}
class MyTriangle implements Triangle{
int side1;
int side2;
int side3;

public MyTriangle(){
this.side1 = 1;
this.side2 = 2;
this.side3 = 3;
}
}
interface Triangle{}

You have not pasted your full code, I assume your code should look something like below.

Then you should create the Instance for your main class before creating instance for your triangle as shown below

public class Test{
class MyTriangle
{
int side1,side2,side3;
public MyTriangle()
{
this.side1 = 3;
this.side2 = 4;
this.side3 = 5;
}

}
public static void main(String[] args)
{
MyTriangle h1 = new Test(). new MyTriangle(); // Fix is here**
}
}

interface Triangle{}

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.

Getting a No enclosing instance of type... error

This is because SAT class is an inner class of BoardTestBean, but not a static inner class. Only static inner classes can be instantiated without an "enclosing" instance context; non-static need a "parent" instance.

If SAT does not need to use any of the BoardTestBean's state, declare it static; otherwise, add an instance method to BoardTestBean and instantiate SAT from there.

P.S. I am assuming that you are accessing SAT from the same package, because it has package visibility. If this is not intentional, you will need to make the class public as well.

EDIT This is how you add an instance method to BoardTestBean returning SAT:

SAT makeSAT() {
return new SAT();
}

Now outside BoardTestBean you can do this:

// assuming that you have an instance of BoardTestBean...
BoardTestBean myBean = ...
BoardTestBean.SAT = myBean.makeSAT();

ERROR: No enclosing instance of type OOPTutorial is accessible

This may not make sense to you if you're new to Java, but a instantiating a non-static inner class (phone) requires an instance of the enclosing class (OOPTutorial).

In plain English, this roughly means that you either

  1. Can only do new phone() inside a OOPTutorial-method that is not marked as static, or

  2. you need to make phone a top level class (i.e. move it outside the scope of OOPTutorial), or

  3. you need to make the inner class phone as static (by putting static in front of the class declaration)

No enclosing instance of type mProgram is accessible. Must qualify the allocation with an enclosing instance of type mProgram

You have to create first an instance of mProgram before creating an instance of an inner class, or you can declare the inner class (NrlData in that case) as static, but you still need the mProgram class to access it (but you don't have to instantiate it.

public class mProgram {
public class NrlData {
...
}

public static void main(String[] args) {
mProgram.NrlData nrlData = new mProgram().new NrlData();
}

public void aMethod() { // accessing inner class from the "this" instance
NrlData nrlData = new NrlData();
}

Or

public class mProgram {
public static class NrlData {
...
}

public static void main(String[] args) {
mProgram.NrlData nrlData = new mProgram.NrlData();
}
}

Just take the first case, where NrlData is not static.

From within a non-static function inside mProgram class, you don't need to create a mProgram instance, because it uses the this instance.

Now if you try to access the inner class from another class, as you don't have any instance of mProgram, you'll have to create first that instance. It's the reason why you have the problem only in NrlMain and not in mProgram.

public class NrlMain {
public void accessingInnerClass() {
// Creating the mProgram instance
mProgram mprogram = new mProgram();
// Creating inner class instance
mProgram.NrlData nrlData = mprogram.new NrlData();
}
}


Related Topics



Leave a reply



Submit