Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Multiple Classes
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
eNc




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Oct 12, 2005 4:05 pm   Post subject: (No 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.
eNc




PostPosted: Wed Oct 12, 2005 6:22 pm   Post subject: (No subject)

thanks wtd, i just wasn't sure if you could do that.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: