0

I created an object within a class:

private class Viscosity{
    float magnitude;
    private Viscosity(float Magnitude){
        magnitude = Magnitude;
    }
}

In my main function, I attempt to extract data from a text file and create a new Viscosity object, but it seems that I cannot access this private object.

For example, I want to add it to a List of Objects:

listofObjects.add(new Viscosity(50.0f));

But I receive the error:

No enclosing instance of type is accessible. Must qualify the allocation with an enclosing instance of type ClassName (e.g. x.new A() where x is an instance of ClassName).

How can I accomplish this?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Why is the constructor private? And why use an inner class? – Hovercraft Full Of Eels Sep 15 '13 at 00:55
  • Where are you trying to invoke `listofObjects.add(new Viscosity(50.0f));`? Is it outside of class that contains `Viscosity` inner class? – Pshemo Sep 15 '13 at 01:00
  • Why don't you post your whole class, not just the inner class? You'll probably get better advice from people that way. – Dawood ibn Kareem Sep 15 '13 at 01:01
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 04 '16 at 00:24

4 Answers4

1

You want to declare that class static if it does not depend on an enclosing instance:

 private static class Viscosity

Or, instead of calling it from the static main method, make an instance of the outer class and move your code into an instance method.

But, really, why does this have to be an inner class? Why not a regular (package-private) class. You can even declare it in the same file if you really want (but that is also not really advisable).

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Can you explain what is meant by enclosing instance? I'm not sure if this helps but I am trying to create multiple Viscosity objects and add them to my list of objects. –  Sep 15 '13 at 00:56
  • In your `static` main method, you don't have `this` (the instance of the outer class). So you cannot access methods, fields or constructors that are not also static. – Thilo Sep 15 '13 at 00:58
1

You should:

  • Make Viscocity a non-inner class. Declare it in its own file, unless you've got a very compelling reason to make it an inner class (which you've not yet made to us).
  • Make its constructor public.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • +1. That is the best advice from a design perspective. Not necessarily public, but maybe package private (default visibility). – Thilo Sep 15 '13 at 01:00
0

Does your inner class need access to any members of the enclosing class? If not, you could make it a static class.

private static class Viscosity {
...
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

You can think of non-static nested class as member of its outer class, so you can use it only via instance of outer class (enclosing). Normally you will firs need to create some outer class instance like `

Outer out = new Outer() 

and then use this object to make instance of its inner class like

Outer.Inner inner = out.new Inner(). 

But to be able to do it your inner class and its constructor will have to be accessible in place you are trying to invoke this code.

There is no problem in using just new Inner() inside non-static methods of Outer class because it will be converted to this.new Inner() and this reference contains current instance of Outer class.
But there is problem with static methods because there is no this inside them, or this can contain wrong instance like if you are invoking new Inner() in class that doesn't extend Outer.

Pshemo
  • 122,468
  • 25
  • 185
  • 269