I have a question in the program below.
Compiling the following programs with javac Main1.java results in a compilation error:
Probably because the Hero class is not nested (not included in the Main 1 class), there are two independent classes, and there are errors.If I don't want to nest classes like this, how can I do it?java
Include the class name .java in the Java source file that contains the Public class declaration.
Compiling the class name .java with javac creates a class file named .class.
Then, the command "java class name" runs the java program with the class name.
Because you rely on the class name in the file name to decide where to run, any discrepancy between the class name and the file name results in an error.
This is a promise made in java, so be sure to keep it.
When you ask questions, you should provide detailed information about your environment.
For Java, state the JDK version.If you are using an integrated development environment, write the name and version.You should also write down the operating system.
Only one Java outermost
public class is allowed to be defined in a single source file.
If you want to define more than one extra
public class, you must partition the source file.
If you want to compile a Java program with multiple source files using javac, see the following:
直感 For intuitive operation and ease of debugging, we recommend using an integrated development environment such as IntelliJ IDEA and Eclipse.
public specification for the class is used when you want to publish the class outside the package.For classes that complete within a package, you can also use the package private with no access level specified.You can define any number of outermost classes of package private in a single file; you can define no
public classes, but only package private classes.
The above is an example of an anonymous package, but since Java 1.4 and later does not allow
import classes to be
public, there is little point in making the class
public.Typically, when defining a
public class, explicitly specify the package name, for example:
com.example.myapp.Main class, you can see the
com.example.myapp.Main class does not show the
You can see the
com.example.mylib.PackagePrivateSub1 class from the
Package segmentation is essential for plug-ins, libraries, and large application development, but requires little awareness at the entry level.
In addition, if you search Google on Java public class, you will get as many hits as you want.Before you ask others questions, you should first visit several introductory sites to organize your information and read the appropriate introductory books to learn the basics of Java systematically and comprehensively.
Remember to do your own research before posting questions.Also, try to ask questions only when you really get stuck
Hero class must be declared in a file named
Hero.java, as shown in the error message in the question statement.
(And the public
Main1 class must be declared in the
Main1.java file (but this is already protected and there is no error).
In other words, you need two files:
Supplement is how to execute from the command line.
To do so, first compile these two files with the
java command then specifies
Main1 where the class contains the
© 2022 OneMinuteCode. All rights reserved.