Retro Gamer
Retro Gamer

Reputation: 1122

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!");
    }
}

Upvotes: 0

Views: 118

Answers (3)

Samolivercz
Samolivercz

Reputation: 220

As long as both are in the same package (folder) there will be no need for the "import Greeter" statement, this should fix it, hope this helps!

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201527

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();

Upvotes: 1

Nicola Ferraro
Nicola Ferraro

Reputation: 4189

Just put the Greeter class in the same folder (i.e. 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 (i.e. in the root folder) you cannot import them.

Upvotes: 1

Related Questions