简体   繁体   中英

Getting Netbeans Java program to compile

I'm new to java, and I've been trying to get my program to compile using Netbeans. HelloWorldApp.java uses the Greeter class in Greeter.java. I keep getting errors and I can't figure it out. I understand that you have to include "packages" or something. I don't have a lot of experience with Netbeans either. But I would love for this to work.

Here is the HelloWorldApp.java:

package helloworldapp;
import Greeter
public class HelloWorldApp
{
    public static void main(String[] args)
    {
        Greeter myGreeterObject = new Greeter();
        myGreeterObject.sayHello();
    }
}

And here is Greeter.java:

public class Greeter
{
    public void sayHello()
    {
        System.out.println("Hello, World!");
    }
}

Just put the Greeter class in the same folder (ie package) as the other file and remove the "import Greeter" statement. You should put every class in a package as you did with the HelloWorldApp class.

If you leave classes without package (ie in the root folder) you cannot import them.

Change the first line of Greeter to

package helloworldapp;

And then remove

import Greeter

from HelloWorldApp . You only need to import classes that are in other packages. Also, an import line is terminated with a semicolon. Finally, import is always optional and a convenience for the developer; as an example,

import java.util.Calendar;

Allows you to write

Calendar cal = Calendar.getInstance();

But, without the import you could still use

java.util.Calendar cal = java.util.Calendar.getInstance();

只要两者都在同一个包(文件夹)中,就不需要“ import Greeter”语句,这应该可以解决,希望对您有所帮助!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM