公共类型<< classname>>>必须在其自己的文件“在Eclipse中出错 [英] "The public type <<classname>> must be defined in its own file" error in Eclipse

查看:282
本文介绍了公共类型<< classname>>>必须在其自己的文件“在Eclipse中出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下代码:

  package staticshow; 


public class StaticDemo {
static int a = 3;
static int b = 4;

static {
System.out.println(Voila!Static block put into action);
}

static void show(){
System.out.println(a =+ a);
System.out.println(b =+ b);
}
}

public class StaticDemoShow {
public static void main(){
StaticDemo.show();
}
}

我收到错误消息:

 公共类型StaticDemo必须在自己的文件中定义

错误在第一行 public class StaticDemo {。为什么会发生,我该如何解决?请注意,我的项目名称为 StaticDemoShow ,程序包名称为 staticshow ,类名与代码中一样。 p>

编辑 - 只做一个类public或两个类都默认,我得到错误Selection不包含主类型。现在我应该怎么做?

解决方案

我们不能在一个文件中有两个公共类。 JVM无法理解,在一个文件中,我们只能写一个公共类。

  public class StaticDemo {

static int a = 3;
static int b = 4;

static {
System.out.println(Voila!Static block put into action);
}

static void show(){
System.out.println(a =+ a);
System.out.println(b =+ b);
}

}
class StaticDemoShow {
public static void main(){
StaticDemo.show();
}

}


I have written the following code:

package staticshow;


public class StaticDemo {
  static int a = 3;
  static int b = 4;

  static {
    System.out.println("Voila! Static block put into action");
  }

  static void show() {
    System.out.println("a= " + a);
    System.out.println("b= " + b);
  }
}

public class StaticDemoShow {
  public static void main() {
    StaticDemo.show(); 
  }
}

I am getting the error message:

The public type StaticDemo must be defined in its own file

error in the very first line public class StaticDemo {. Why is it happening and how can I resolve it? Note that my project name is StaticDemoShow, package name is staticshow and class names are as given in the code.

EDIT- After making just one class public or both the classes default, I am getting the error "Selection does not contain a main type". Now what should I do?

解决方案

We cant have two public classes in one file. The JVM cannot understand, in one file we must write one public class only.

public class StaticDemo {

    static int a = 3;
    static int b = 4;

    static {
        System.out.println("Voila! Static block put into action");
    }

    static void show() {
        System.out.println("a= " + a);
        System.out.println("b= " + b);
    }

}
 class StaticDemoShow {
    public static void main() {
        StaticDemo.show();
    }

}

这篇关于公共类型<< classname>>>必须在其自己的文件“在Eclipse中出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆