0
public class constructor {

    class Person {
        private String name;
        private String birthday;
        private String sex;

        public Person( String name, String birthday, String sex) {
            this.name = name;
            this.birthday = birthday;
            this.sex = sex;
        }        
    }


    public static void main(String[] args) {
        Person obj=new Person("safwan","01--05-1999","male");

    }

}

Exception in thread "main" java.lang.Error: Unresolved compilation problem: No enclosing instance of type constructor is accessible. Must qualify the allocation with an enclosing instance of type constructor (e.g. x.new A() where x is an instance of constructor).

at constructor.main(constructor.java:18)

Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
Safwan Bardolia
  • 569
  • 1
  • 4
  • 12
  • Your `Person` class is an inner class so it has to have a reference to instance of `constructor` class (underneath). Move `Person` class to another java file or move `Person` class declaration outside of `constructor` class body, or add `static` to `class Person`. – Michał Krzywański Aug 02 '19 at 15:32
  • 1
    I think naming a class `constructor` is one of the worst names I have ever seen. Please follow proper Java naming conventions (class should be uppercase like your `Person`), and do not name something so confusing. – Nexevis Aug 02 '19 at 15:35

1 Answers1

0

You could make your Person class static:

public class constructor {

    static class Person {

        private String name;
        private String birthday;
        private String sex;

        public Person( String name, String birthday, String sex) {
            this.name = name;
            this.birthday = birthday;
            this.sex = sex;
        }        
    }

    public static void main(String[] args) {

        Person obj=new Person("safwan","01--05-1999","male");
    }
}

so it will compile.

Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52