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.
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:erlDesignPatterns)[/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.
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.