Jump to content

Compiling an Application with Multiple Classes

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
neophyte

neophyte

    Newbie

  • Members
  • Pip
  • 6 posts
Greetings!

I'm using NetBeans 6.5 and the textbook Java How To Program 7th edition by Deitel & Deitel. I created two new projects: PanelFrame.java (p. 590) and PanelDemo.java (p. 591). PanelFrame.java doesn't contain method main.

The book says to compile both classes at once -
javac.PanelFrame.java PanelDemo.java

How do I accomplish the same thing with NetBeans?

When I create a new project in NetBeans I do the following:

File, New Project, Category: Java,
Projects: Java Application, Project Name: PanelFrame,
Create Main Class: panelframe.PanelFrame ( Create Main Class - checked),
Set as Main Project (checked)

Maybe I'm not doing something right when I create a new project.

Thanks in advance!

Mark

#2
cdg10620

cdg10620

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 389 posts
You can create classes in java that contain fields, properties, and methods but when you compile the compiler is looking for a class called "Main." It looks for this because the "Main" is where the code is actually ran. Inside of Main you can create instances of objects, use methods, and have output to the user. Does that make sense?

for example
import java.util.*;


public class User 

{

    private String Name;

    public static int Age = 0;


    public User( String name, int age) 

    {

      this.Name = name;

      this.Age = age;

    }


    public String getName() 

    {

      return this.Name;

    }

    

    public int getAge()

    {

   		return this.Age;

    }


    public static void main(String[] args)

    {

		User student = new User( "James", 23 );

		System.out.println( student.getName() );

		System.out.println( student.getAge() );

    }

}


Feel free to ask more questions.

#3
neophyte

neophyte

    Newbie

  • Members
  • Pip
  • 6 posts
Thanks for the reply! It makes sense to me that the compiler is looking for class main (I thought it was a method). The book states that you have to compile the applications at the same time. That doesn't make sense to me because what if I want to use my new class in ten applications. Does that mean I have to compile eleven applications at the same time?

I'm new at this so I'm confused. My understanding of this so far is that I should be able to compile my new class one time and then I'll be able to use it with any other application as long as that application knows where to find it on the computer. Do I have to create a package which includes my new class in order to do this?

Thanks in advance for any help!

#4
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts

neophyte said:


I'm new at this so I'm confused. My understanding of this so far is that I should be able to compile my new class one time and then I'll be able to use it with any other application as long as that application knows where to find it on the computer. Do I have to create a package which includes my new class in order to do this?

Yes.

#5
cdg10620

cdg10620

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 389 posts
Each class should be in its own file, properly named, for organization purposes.