java - Student a=new Student() 错误;

标签 java

<分区>

出现如下错误:

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)

以下是我的代码:

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;

    }

}

最佳答案

您的 Student 类不是静态的,您正试图从不允许的静态上下文访问它。​​

您的代码应如下所示:

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;

    }

}

关于java - Student a=new Student() 错误;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40585598/

相关文章:

java - 如何在 Android 中使用 SQLite 从微调器中根据选定的月份和年份从列中获取值?

Java,时间,事件

java - 为什么我会收到此 "unreachable statement"错误?

java - 如何使用JAVA句柄createStatement INNODB更新sql varchar列

java - 将大科学数转换为长数

java - 使用 Java 对带有 XML 的 wav 文件进行分段

java - 什么时候应该重写Java中的equal方法

java - 如何告诉 hadoop 有多少内存分配给单个映射器作业?

java - 如何使用正则表达式匹配一个字符串中所有可能的情况?

java.lang.NoClassDefFoundError : org/lwjgl/LWJGLException 错误