Hi I am having problems running and compiling a program with more than one class, but I am not sure how to do it in netbean, it keeps complaining. I have the 2 following files called respectively Account.java

public class Account {
    String name;
    String address;
    double balance;
}

and UseAccount.java

import static java.lang.System.out;

class UseAccount {

    public static void main(String args[]) {
        Account myAccount;
        Account yourAccount;

        myAccount = new Account();
        yourAccount = new Account();

        myAccount.name = "Barry Burd";
        myAccount.address = "222 Cyberspace Lane";
        myAccount.balance = 24.02;

        yourAccount.name = "Jane Q. Public";
        yourAccount.address = "111 Consumer Street";
        yourAccount.balance = 55.63;

        out.print(myAccount.name);
        out.print(" (");
        out.print(myAccount.address);
        out.print(") has $");
        out.print(myAccount.balance);
        out.println();

        out.print(yourAccount.name);
        out.print(" (");
        out.print(yourAccount.address);
        out.print(") has $");
        out.print(yourAccount.balance);
    }
}

With simple programs - one class only - I usually go FIle, New Project, select Java under categories and java application under Project, that's all. SO, in the case above I would have UseAccount.java as main class but then I don't know where to put the second one.
thanks

Recommended Answers

All 5 Replies

That seems to be compiling in my IDE anyway. Just ensure that both .java files are in the same package (directory) and run the project.

Creating a project with multiple classes isn't any different from creating a project with one class. Once the classes are public and in the same package, they should be able to integrate with each other.

If you're still having problems, post the compiler error you're getting when you run it.

put both the files in same folder/directory.
When any classes is in same directory(Package) you can access it just by creating object.

If you are using netbeans IDE,make sure that both files are in src folder.

yes it is fine, I had troubles inserting the class without main in the structure somewhere not having done it before. So it looks like when I start a new project the first class that gets added is the one with a main method then, if I right click on package I can add a new class (and this by default has no main method). Then the program runs ok and compiles. I was more interested in the order you do things: the book I am reading said to add the class without main first then the other one, but I found that the first class added to a project has a main method, so it will have to come first. I think this was what confused me
thanks

Basically, you can add the classes in whatever order you want. Just make sure that the class you want to run is the one with the method

public static void main(String[] args) {

When you run the project, that is the only method which will be called. So, like in your example, this is where the main functionality of your project is handled.

In NetBeans, it will automatically insert that method into the first class you create. If you want to have another class act as the main class, just cut and paste that method into that class.

fab, thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.