1

I am getting error as following :

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

No enclosing instance of type New is accessible. Must qualify the allocation with an enclosing instance of type New (e.g. x.new A() where x is an instance of New). at n.New.main(New.java:7)

Following is my code :

package n;

public class New 
{

    public static void main(String[] args) 
    {
        Student a=new Student();
        a.name="abc";
        a.number=6;
        a.marks=1;

        System.out.println(a.name);
        System.out.println(a.number);
        System.out.println(a.marks);
    }

    class Student
    {
        String name;
        int number;
        int marks;

    }

}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76

5 Answers5

2

Your class Student is not static and you are trying to access it from static context which is not allowed.

Your code should be as follows:

package n;

public class New {

    public static void main(String[] args) {
        Student a = new Student();
        a.name = "abc";
        a.number = 6;
        a.marks = 1;

        System.out.println(a.name);
        System.out.println(a.number);
        System.out.println(a.marks);
    }

    static class Student {
        String name;
        int number;
        int marks;

    }

}
Chirag Parmar
  • 833
  • 11
  • 26
1

The class Student is a non-static inner class of the class New, and must be accessed from an object of the class New. It cannot be accessed directly from main as main is static and Student is not. As a solution, just declare it outside New like this:

package n;

public class New {

    public static void main(String[] args) {
        Student a = new Student();
        a.name = "abc";
        a.number = 6;
        a.marks = 1;

        System.out.println(a.name);
        System.out.println(a.number);
        System.out.println(a.marks);
    }
}

class Student {
    String name;
    int number;
    int marks;

}
AhmadWabbi
  • 2,253
  • 1
  • 20
  • 35
1

The compilation error happens because Student is an inner class of New.

As Student is defined as class Student, it can only exist inside of an instance of New. So there are a few ways to solve this "problem".

The most simple one: make it

static class Student 

Due to this, a single instance of Student isn´t bound to exist in an instance of New.

Another one is creating an instance of New where you create an instance of Student:

Student a = new New().new Student();

But for the purpose of starting to learn how to program, I'd say: remove the inner Student class and start with Student being the outer class.

public class Student {
    String name;
    int number;
    int marks;

    public static void main(String[] args) {
        Student a = new Student();
        a.name = "abc";
        a.number = 6;
        a.marks = 1;

        System.out.println(a.name);
        System.out.println(a.number);
        System.out.println(a.marks);
    }
}

Some additions for the starter:

include a constructor in Student as Student(String, int, int) and call it when initializing a new instance of Student, instead of initializing it and setting the variables afterward:

new Student("abc", 1, 6); 
Jeffmagma
  • 452
  • 2
  • 8
  • 20
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
0

You inner class should be static as you are trying to access it from static context which gives error.

static class Student {
        String name;
        int number;
        int marks;

    }
Mandeep Singh
  • 1,287
  • 14
  • 34
0

Two options:

  1. make the Student class static
  2. initialize the outer class first and then initialize the inner class like this:

    New n = new New(); Student a=n.new Student();

See https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html for more details.

Kojotak
  • 2,020
  • 2
  • 16
  • 29