Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An enclosing instance that contains <my reference> is required

Tags:

java

instance

An enclosing instance that contains is required

Below is the code. positionObj is the object that I am trying to use and it is giving me the above error.

It's unclear why.

package toolBox;
import toolBox.Secretary.positionObj;    

public class PositionManagement {
    public static HashMap<String, Secretary.positionObj> main(String vArg){
        positionObj newPosition=new positionObj();
    }
}
like image 763
jason m Avatar asked Nov 28 '10 16:11

jason m


2 Answers

You're trying to use the non-static inner positionObj class without an instance of Secretary for it to belong to.
A non-static inner class must belong to an instance of its parent class

You should probably change positionObj to a normal class or a static inner class.

Alternatively, you can write someSecretary.new positionObj() to create an instance of the inner class that belongs to the someSecretary instance.

like image 176
SLaks Avatar answered Oct 17 '22 15:10

SLaks


First create an object of Outer class. In this case I think "Secretary". Then create positionObj. Like this,

Secretary x = new Secretary();
Secretary.positionObj y = x.new positionObj();
like image 37
Teshan Avatar answered Oct 17 '22 16:10

Teshan