j2me 错误:类 myClass 是 public 应该在名为 myClass.java 的文件中声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5728007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:30:02  来源:igfitidea点击:

j2me error: class myClass is public should be declared in a file named myClass.java

javajava-me

提问by learnerNo1

Guys, how can I correct the error above, my class name does have the extension.java. I am new to j2me and I need to do a location based application but I am struggling so hard...

伙计们,我该如何纠正上面的错误,我的类名确实有 extension.java。我是 j2me 的新手,我需要做一个基于位置的应用程序,但我很努力......

回答by king_nak

Generally in Java, each .java file can contain at most one public top-level class, and the file has to have the same name as this class.
So a class called MyClassmustbe stored in a file called MyClass.java!

一般在Java中,每个.java文件最多可以包含一个公共顶级类,并且该文件必须与这个类同名。
因此,一个名为的类MyClass必须存储在一个名为MyClass.java!

NB: Class names should start with a capital letter

注意:类名应该以大写字母开头

Update:when using packages (the packageinstruction at the beginning of the java file), the file must be stored in a directory structure identical to the package structure.
So if you have this file:

更新:使用包时(packagejava文件开头的说明),文件必须存放在与包结构相同的目录结构中。
所以如果你有这个文件:

package com.something.test;
public class MyClass {}

it must be stored in {root}/com/something/test/MyClass.javaand compiled with
javac com.something.test.MyClass
relative from the directory {root}

它必须存储在 目录中{root}/com/something/test/MyClass.java并使用
javac com.something.test.MyClass
相对目录进行编译{root}

回答by Rosie

In NetBeans 6.5, I fixed this problem by following the first paths on the left down to:

在 NetBeans 6.5 中,我通过遵循左侧的第一条路径解决了这个问题:

fileName > Source Packages > filename > Main.java

文件名 > 源包 > 文件名 > Main.java

Right-click on Main.java, move the mouse over Refactor, then click Rename. Type the class name there (it IS case-sensitive) and select Refactor. This should solve the problem.

右键单击 Main.java,将鼠标移到 Refactor 上,然后单击 Rename。在那里输入类名(区分大小写)并选择 Refactor。这应该可以解决问题。