1

I am a freshman,I can't understand what's the"no closing instance of....." means?

public static void main(String[] args) {
    Map<Pk_person, Person> map = new HashMap<Pk_person, Person>();
    Pk_person pk_person = new Pk_person(); //Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    //No enclosing instance of type Simple1 is accessible. 
    //Must qualify the allocation with an enclosing instance of type Simple1 (e.g. x.new A() where x is an instance of Simple1).
    pk_person.setPrefix("MR");
    pk_person.setNumber(22081);
    map.put(pk_person, new Person(pk_person, "马先生"));
    /*Pk_person pk_person2 = new Pk_person();
    pk_person2.setPrefix("MR");
    pk_person2.setNumber(22081);
    Person person2 = map.get(pk_person2);*/
    System.out.println(pk_person.getPrefix());      
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Barry Bao
  • 29
  • 5
  • 2
    Well presumably `Pk_person` is an inner class. You could either make it a static nested class, or make it a top-level class. (I'd also strongly urge you to rename it to a more conventional name, but that's a different matter...) – Jon Skeet Aug 15 '15 at 12:42
  • 1
    You have not shown the definition of `Pk_person` (the name should be `PkPerson` if it is to conform with language coding conventions, by the way), but I'm pretty sure it is an inner class (non-static) and therefore, cannot be created without having an enclosing instance. – RealSkeptic Aug 15 '15 at 12:43
  • ``You are missing `static` in front of `class Pk_person`.`` – Sergey Kalinichenko Aug 15 '15 at 12:44
  • 1
    I make it a static nested class and it works,though I still don't know exactly why.But thank you very much, – Barry Bao Aug 15 '15 at 12:56
  • 1
    [This thread](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) explains the problem. – Mick Mnemonic Aug 15 '15 at 13:12

2 Answers2

0

Pk_person is an inner class of class Simple1 .Just like member varibales cannot exist without an instance of Class,similarly inner class need an enclosing of an outer class's instance.They can have any access modifier just like other member varibales.

You need to do something like this:-

new Simple1().new Pk_person();
Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
0

The Oracle Docs give quite a good explanation why things work / won't work that way. Also other threads as pointed out by Mick Mnemonic give some explanations. Instead of repeating them, consider the analogy with static variables:

You cannot refer to a non-static variable from a static context (like the main() method cannot access a variable in the same class that is not static). And likewise you cannot refer to a non-static nested class from a static context.

That's because from a nested inner ( = non-static) class you can access (non-static) member variables of the enclosing (outer) class, which makes it impossible to refer to an instance of an inner class from a static context.

Community
  • 1
  • 1
beosign
  • 433
  • 5
  • 10