Results 1 to 8 of 8
  1. #1
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default 2 classes in the same file

    Hi,

    I've tried to make a program with 2 classes, while the 2 classes are in the same file.

    It's kinda weird, but it means I've opened a new class, with the "main" in it, and then tried to add another class into the same page below (2 classes in the same class ?! -_-).

    It didn't work, so I wanted to know how exactly am I supposed to do it : \

    Thanks.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: 2 classes in the same file

    You can have many class definitions in a file, but only one can be public and it must have the same name as the filename. The compiler will generate a .class file for each class definition in the file.
    Any and all classes can have a main() method. There is no restriction on that.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: 2 classes in the same file

    The reason is to allow to define multiple classes in a one *.java file is to bundle the related supported functionality together with your main class. However, as Norm said you can have only one public class in the file and it could be the main class in many cases.

    That kind of coding increase the readability as well (but not always).

  5. #5
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: 2 classes in the same file

    Well, after trying what you said, it worked.
    I erased the "public" from the second. I've got confused with the main also, I forgot it was a method and not a class. What I really meant was the "calling" class that calls another class in order to make objects from it.

    But still, "public" means that the class can be called by all the other classes in the same package, right? So what's wrong about 2 public classes?

    Thanks for the help :)

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: 2 classes in the same file

    You can make many/all classes public, but only one per source file.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: 2 classes in the same file

    Quote Originally Posted by Mapisto View Post
    Well, after trying what you said, it worked.
    I erased the "public" from the second. I've got confused with the main also, I forgot it was a method and not a class. What I really meant was the "calling" class that calls another class in order to make objects from it.

    But still, "public" means that the class can be called by all the other classes in the same package, right? So what's wrong about 2 public classes?

    Thanks for the help :)
    Assume that you have a one source file having two classes. Then the structure looks like;

    Java Code:
    public class MyClassOne { // Note: Your source file name must the same as the public class
         
         public static void main(String args[]) {
               // main method logic goes here
    
              MyClassTwo obj = new MyClassTwo();
         }
    
    }
    
    class MyClassTwo {
    
          // MyClassTwo related stuff goes here
    
    }

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 1
    Last Post: 08-04-2011, 10:22 AM
  2. Replies: 59
    Last Post: 05-22-2011, 09:37 AM
  3. debug classes in jar file
    By hghayoumi in forum Java Servlet
    Replies: 0
    Last Post: 11-29-2009, 04:54 PM
  4. Compiling and using jar file for custom classes
    By MAILMIRZA in forum New To Java
    Replies: 3
    Last Post: 01-12-2009, 05:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •