Easy Java question

Associate
Joined
24 Sep 2003
Posts
928
Location
Bedfordshire
Hi,

I haven't done Java in a while (been using C++ a bit) and I've forgotten how to easily use multiple java files for a project and call methods in the different files.

In C++ I'm obviously just including header files with method stubs in to get access to them in the relevant file, how do I do a similar thing in Java?

Thanks,

aaazza
 
Unless your classes are in the same package you first have to import them by using an import statement. For example:

Code:
import java.util.Random;

You then need to obtain an instance of the class unless it is a static class. You can do this by making a call to its constructor:

Code:
Random rand = new Random();

You can then use the instance to make calls:

Code:
int num = rand.nextInt();
 
Back
Top Bottom