An enclosing instance that contains <my reference> is required

亡梦爱人 提交于 2019-11-26 10:59:48

问题


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();
    }
}

回答1:


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.




回答2:


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();



回答3:


The correct generic signature would be

public static HashMap<String, positionObj> main(String vArg)

you dont need to qualify positionObj since you already import it.

However, I am pretty sure a main method must conform to the signature below. If you intend to have main be the main method for your program, change the signature to

 public static void main(String[] args) {...}

you can create a separate static method that returns a Map and invoke it from main.

As a note, all classes should begin with a capital letter, positionObj, should be PositionObj.



来源:https://stackoverflow.com/questions/4297857/an-enclosing-instance-that-contains-my-reference-is-required

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!