Java 学生a处出错=新学生();

Java 学生a处出错=新学生();,java,Java,我得到如下错误: 线程“main”java.lang中出现异常。错误:未解决的编译问题: 无法访问类型为New的封闭实例。必须使用New类型的封闭实例限定分配(例如,x.New A(),其中x是New的实例)。 位于n.New.main(New.java:7) 以下是我的代码: package n; public class New { public static void main(String[] args) { Student a=new Stude

我得到如下错误:

线程“main”java.lang中出现异常。错误:未解决的编译问题:

无法访问类型为New的封闭实例。必须使用New类型的封闭实例限定分配(例如,x.New A(),其中x是New的实例)。 位于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;

    }

}

您的学生不是静态的,您正试图从静态上下文访问它,这是不允许的

您的代码应如下所示:

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;

    }

}

您的学生不是静态的,您正试图从静态上下文访问它,这是不允许的

您的代码应如下所示:

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;

    }

}

Student
是类
New
的非静态内部类,必须从类
New
的对象访问。它不能直接从
main
访问,因为
main
是静态的,而
Student
不是。作为解决方案,只需在
New
之外声明它,如下所示:

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
是类
New
的非静态内部类,必须从类
New
的对象访问。它不能直接从
main
访问,因为
main
是静态的,而
Student
不是。作为解决方案,只需在
New
之外声明它,如下所示:

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
New
的内部类

由于
Student
定义为
class Student
,因此它只能存在于
New
的实例中。因此,有几种方法可以解决这个“问题”

最简单的一个:让它

static class Student 
因此,
Student
的单个实例不一定存在于
New
的实例中

另一个是创建
New
的实例,您可以在其中创建
Student
的实例:

Student a = new New().new Student();
但是为了开始学习如何编程,我会说:去掉内部的
Student
类,从外部的
Student
类开始

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);
    }
}
起动机的一些附加部件:

将构造函数作为
Student(String,int,int)
包含在
Student
中,并在初始化
Student
的新实例时调用它,而不是在初始化后设置变量:

new Student("abc", 1, 6); 

发生编译错误的原因是
Student
New
的内部类

由于
Student
定义为
class Student
,因此它只能存在于
New
的实例中。因此,有几种方法可以解决这个“问题”

最简单的一个:让它

static class Student 
因此,
Student
的单个实例不一定存在于
New
的实例中

另一个是创建
New
的实例,您可以在其中创建
Student
的实例:

Student a = new New().new Student();
但是为了开始学习如何编程,我会说:去掉内部的
Student
类,从外部的
Student
类开始

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);
    }
}
起动机的一些附加部件:

将构造函数作为
Student(String,int,int)
包含在
Student
中,并在初始化
Student
的新实例时调用它,而不是在初始化后设置变量:

new Student("abc", 1, 6); 

当您试图从静态上下文访问内部类时,内部类应该是静态的,这会导致错误

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

    }

当您试图从静态上下文访问内部类时,内部类应该是静态的,这会导致错误

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

    }
两种选择:

  • 使学生课堂保持静止
  • 首先初始化外部类,然后像这样初始化内部类:

    新的n=新的新的(); 学生a=n.新生

  • 有关更多详细信息,请参阅。

    两个选项:

  • 使学生课堂保持静止
  • 首先初始化外部类,然后像这样初始化内部类:

    新的n=新的新的(); 学生a=n.新生



  • 有关更多详细信息,请参阅。

    @SantoshReddy您真的阅读了错误消息吗?@magic sudo这两条评论都是错误的,实际上不需要做,也不是编译错误的原因。这是怎么回事。实际上我是一个初学者。您能告诉我这些更改吗?我会将类似Student的课程放在一个新的类文件中。使它们更容易访问并且不会出现此类错误您需要添加如下
    静态
    关键字:
    静态类学生
    @SantoshReddy您是否确实阅读了错误消息?@magic sudo这两条评论都是错误的,实际上不需要这样做,编译错误不是错误吗?这是怎么回事?事实上我是个初学者。你能告诉我这些变化吗?我会把像Student这样的课程放在一个新的类文件中。使它们更容易访问并且不会出现此类错误您需要添加如下
    static
    关键字:
    static class Student
    谢谢@chirag parmar。“我明白了。”SantoshReddy一般来说,创建静态类不是一个好的实践。您可以使用外部类的引用访问内部类。。你真的需要一个内部类吗?是的……我同意@iMBMT.。只是简单提醒一下:不要将“编辑:”之类的短语编辑成你编辑的问题/答案。这被认为是噪音。哦…@Seth..我会小心的..)谢谢。。!!谢谢@chirag parmar。“我明白了。”SantoshReddy一般来说,创建静态类不是一个好的实践。您可以使用外部类的引用访问内部类。。你真的需要一个内部类吗?是的……我同意@iMBMT.。只是简单提醒一下:不要将“编辑:”之类的短语编辑成你编辑的问题/答案。这被认为是噪音。哦…@Seth..我会小心的..)谢谢。。!!