How to Run Programs for Two Public Classes

Asked 2 years ago, Updated 2 years ago, 33 views

I have a question in the program below.
Compiling the following programs with javac Main1.java results in a compilation error:
Error Contents

Main1.java:20: Error: Class Hero is public and must be declared in file Hero.java
        public class Hero {
               ^

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?

import java.util.*;
import java.awt.event.*;
import javax.swing.*;



    public class Main1 {
        public static void main(String[]args) {
            Heroa = new Hero();

            a.name = "Pikachu";
            a.hp = 100;

            System.out.println ("Brave" + a.name + "created!");

            a.sit(5);
        }
      }

        public class Hero {
          String name;
          inthp;
          int level = 10;
      
          public void sleep() {
            This.hp = 100;
            System.out.println(this.name+"is back to sleep!");
          }
      
          public void sit(intsec){
            This.hp+=sec;
            System.out.println (where this.name+" is "+sec+"sit down");
          }
      }

java

2022-09-30 11:04

3 Answers

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.


2022-09-30 11:04

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.

//Main.java
public class Main {
...
}
//Sub1.java
public class Sub1 {
...
}

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.

However, the 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.

//Main.java
public class Main {
...
}

classSub1 {
...
}

classSub2 {
...
}

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.java
package com.example.myapp;

public class Main {
...
}
//com/example/mylib/Sub1.java
package com.example.mylib;

public class Sub1 {
...
}

classPackagePrivateSub1 {
...
}

From the com.example.myapp.Main class, you can see the com.example.mylib.Sub1 class.
The com.example.myapp.Main class does not show the com.example.mylib.PackagePrivateSub1 class.
You can see the com.example.mylib.PackagePrivateSub1 class from the com.example.mylib.Sub1 class.

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


2022-09-30 11:04

The public 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:

Main1.java:

public class Main1 {
    public static void main(String[]args) {
        Heroa = new Hero();

        a.name = "Pikachu";
        a.hp = 100;

        System.out.println ("Brave" + a.name + "created!");

        a.sit(5);
    }
}

Hero.java:

public class Hero{
    String name;
    inthp;
    int level = 10;

    public void sleep() {
        This.hp = 100;
        System.out.println(this.name+"is back to sleep!");
    }

    public void sit(intsec){
        This.hp+=sec;
        System.out.println (where this.name+" is "+sec+"sit down");
    }
}

Supplement is how to execute from the command line.

To do so, first compile these two files with the javac command:

javac-d classes Main 1.java Hero.java

The java command then specifies Main1 where the class contains the main method:

java-cp classes Main 1

Argument details of the command are provided in the official documentation (javac, java).


2022-09-30 11:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.