Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I reference an object located in a different project? 3

Status
Not open for further replies.

Enea

Technical User
Feb 23, 2005
90
0
0
US
I am new to Java.
I created:
Project: Learning
Class: Bikes


Now I just created a new project. I wish to create a new Bikes object called Motorino
Bikes Motorino= new Bikes(model, color);

How do I reference my class Bikes located in a different project?
 
You would make the Bikes class available on the classpath for the new project. How that is done depends upon the tool you are using to write your java, which you didn't specify.

If you are simply using a text editor and compiling by hand, then you would use
Code:
javac -classpath [i]"path to base directory of your class"[/i] ...

when compiling. If you didn't specify a package for your Bikes class, then the base directory is the same directory where the class is located. If you did, then the path is the topmost directory of the package hierarchy where the class is located.

Tim
 
Enea

This may seem a bit pedantic, but you should really name your class Bike rather than Bikes - each instance only represents one bike, whereas 'Bikes' kind of implies a collection of Bike objects.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Steve, I for one don't view your comment as pedantic. Naming a class correctly helps you to think about it in the correct sense, IMO. Learning how to name classes effectively is something fundamental to good OO design, and as such should be appreciated earlier rather than later.

Tim
 
Thank you Tim for your reply. I am using Eclipse.
Steve, I now clearly understand why my class name should be Bike instead of Bikes and I thank you for pointing that out.

 
Sorry Tim, I tested it and I am still not clear. I deleted everything and started fresh. Here is what I have now:

-----------------------
IN PROJECT LEARNING
-----------------------
public class Bike {

public static void action(){
System.out.println("Rolling down the street..");
}
}

--------------------
IN PROJECT TESTING
--------------------

public class TestDrive {

public static void main(String[] args) {

Bike.action();

}
}



TestDrive does not know where to find Bike.
The folder where all my projects are is:
C:\My Documents\Java

Do I need to add a line at the beginning of public class TestDrive to specify the path.
I know that when a class is in a different package we do an import. (import packageName.class)
But when the object is in a different project, I am still confused on what to do.

 
Use project properties (Right click > Preferences) to add the second project to the Java Build Path of the first project if you're planning to compile and execute it in Eclipse.

Cheers,
Dian
 
It worked. Thank you Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top