You must create a custom class in JAVA

I want to create a custom class object in JAVA, and I created it, but it shows an error ... does not know why this error occurs, please help me because I am starting to learn JAVA earlier ...


  class main {

    class student {
        public int rollno;
        public String name;
        public int marks;

        public void accept() {
            rollno = 1;
            name = "Pawan Mall";
            marks = 100;
        }

        public void display() {
            System.out.println(rollno);
            System.out.println(name);
            System.out.println(marks);
        }

    }

    public static void main(String argv[]) {
        student s = new student();
        s.accept();
        s.display();
    }

}

This happened during compilation; this is an error that I encountered when compiling the code:

C:\Program Files\Java\jdk1.7.0_03\bin\student.java:28: error: non-static variable this cannot be referenced from a static context
student s = new student();
            ^
1 error

Tool completed with exit code 1
+3
source share
3 answers

Your class is studentnested in the class main. Since you did not declare it as static, it is therefore a class inner. The Java tutorial says that:

An InnerClass instance can exist only in an OuterClass instance.

Since this is exactly what you are trying to do, it fails.

student , .

class main {

    static class student {
        public int rollno;
+2

, ,

main m = new main();

student s= m.new student(); 
+1

The first char of the name class must be capital. The class name and file name are the same. In your case: "student" → "Student"

-1
source

All Articles