Computer Science Canada

Multiple Classes

Author:  eNc [ Wed Oct 12, 2005 2:04 pm ]
Post subject:  Multiple Classes

How can I have 2 files, 1 with a class containing my main method, and then the other with an object class, and then using my main method file to create a new object which is defined in the other file.

Author:  wtd [ Wed Oct 12, 2005 4:05 pm ]
Post subject: 

An example.

In "Foo.java":

Java:
public class Foo
{
   private int bar;

   public Foo(int initBar)
   {
      bar = initBar;
   }

   public String toString()
   {
      return Integer.toString(bar);
   }
}


And in "Main.java":

Java:
public class Main
{
   public static void main(String[] args)
   {
      Foo f = new Foo(42);
      System.out.println(f);
   }
}


As long as both are in the same directory, that just works.

Author:  eNc [ Wed Oct 12, 2005 6:22 pm ]
Post subject: 

thanks wtd, i just wasn't sure if you could do that.


: