How to fix below error? I need quick help.
Exception in thread "main" java.lang.Error: Unresolved compilation problem: No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main).
at com.common.Main.main(Main.java:16)
Here is my code for reference. If we fixed that this issue then "John"
should be printed only once and not twice. How we can restrict "John"
to print once
public class Main {
public static void main(String[] args) {
Set<Person> l = new HashSet<Person>();
l.add(new Person("John"));
l.add(new Person("John"));
l.add(new Person("Matt"));
l.add(new Person("Deepak"));
l.add(new Person("Chris"));
for (Iterator iterator = l.iterator(); iterator.hasNext();) {
Person person = (Person) iterator.next();
System.out.println(person.getName());
}
}
public class Person{
private String name;
public Person(String name) {
super();
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}