【问题标题】:Error at Student a=new Student(); [duplicate]学生错误 a=new Student(); [复制]
【发布时间】:2017-03-27 21:42:38
【问题描述】:

我收到如下错误:

线程“main”java.lang.Error 中的异常:未解决的编译问题:

没有可访问类型 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;

    }

}

【问题讨论】:

  • @SantoshReddy 你真的阅读错误信息了吗?
  • @magic-sudo 两个 cmets 都是错误的,实际上不需要这样做,也不是编译错误的原因
  • 那是怎么回事。其实我是个初学者。你能告诉我变化吗?
  • 我会将像 Student 这样的类放在一个新的类文件中。使它们更易于访问并且不会出现此类错误
  • 你需要像这样添加static关键字:static class Student

标签: java


【解决方案1】:

你的学生类不是静态的,你试图从不允许的静态上下文中访问它。

你的代码应该如下:

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。我明白了。
  • @SantoshReddy 通常,制作静态类不是好习惯。要么你通过引用外部类来访问内部类。你真的需要一个内部类吗?
  • 是的...我同意@iMBMT ..
  • 请注意:不要将“编辑:”之类的短语编辑到您编辑的问题/答案中。它被认为是噪音。
  • 哦...@Seth ..我会照顾好..:) 谢谢..!!
【解决方案2】:

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;

}

【讨论】:

    【解决方案3】:

    出现编译错误是因为StudentNew 的内部类。

    由于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 中包含一个构造函数作为Student(String, int, int),并在初始化Student 的新实例时调用它,而不是在之后初始化它并设置变量:

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

    【讨论】:

      【解决方案4】:

      你的内部类应该是静态的,因为你试图从静态上下文中访问它会出错。

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

      【讨论】:

        【解决方案5】:

        两种选择:

        1. 将 Student 类设为静态
        2. 先初始化外部类,再初始化内部类,如下:

          新 n = new New(); 学生 a=n.new Student();

        更多详情请见https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

        【讨论】:

          猜你喜欢
          • 2021-12-07
          • 2016-04-19
          • 2023-03-23
          • 1970-01-01
          • 1970-01-01
          • 2020-02-14
          • 2016-03-13
          • 2019-01-14
          • 2012-01-15
          相关资源
          最近更新 更多