Re: Initiating a class
christopher_board@yahoo.co.uk wrote:
hi all.
I am trying to write a program that has a separate class file to the
rest of the functions within the program. How would I go about making
a section of the program run what it is inside the class.
Any help in this matter would be appreciated
As Kal said we really don't have enough information. It would be
helpful for you to explain more about what is going on. This is either
really easy to do, or really really hard. :-)
For really easy, you can just import a class. If you do this:
import java.util.Date;
class Main {
static public void main( String [] args ) {
Date d = new Date();
System.out.println( d ); // What's the default date?
}
}
You've just imported a separate class file, Date.class (compiled from
Date.java) into your program and run it's toString() method. (Note: I
didn't check to make sure the above compiles.)
You can do the same thing on your own. Place two public classes in
separate .java files in the same directory (don't use a package
statement inside either class). Compile and run from that same
directory. Have one invoke the other (no import needed). It should
work fine since both will use default package space.
It works the same if you do use a package command, and you place the
..java files in the appropriate part of your CLASSPATH variable. Just
name the files fully ( "java.util.Date d = new java.util.Date();" ) or
import them and it'll all just work.
Now for wholly separate .jar files, or libraries that aren't on your
classpath but need to be downloaded automagically from a website, or
running wholly separate programs, it's different. We'll need much more
info about exactly what you're trying to do.