Results 1 to 8 of 8
  1. #1
    b0rt is offline Member
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Question Declaring classes in the same .java

    Hi there,

    I have a doubt of basic Java. I always thought that if you declared a class inside another class file - not nesting it, but using the same file - that class was for private use of the public one:

    Java Code:
    // file MyClass1.java
    
    package myclasses;
    
    public class MyClass1{
        //class stuff
    
    }
    class MyClass2{
       //class stuff
    }
    But now I discovered that if I did this in a second java file, the compiler tells me MyClass2 is already defined in that package... Why then only one class can be defined public in one file if in the end all of them can be used outside the file?

    Java Code:
    // file MyClass3.java
    
    package myclasses;
    
    public class MyClass3{
        //class stuff
    
    }
    [COLOR="red"]class MyClass2{[/COLOR] // Compiler error: Already defined in package myclasses
       //class stuff
    }
    I have tested that I can use MyClass2 freely within different classes working fine. I find this very strange, since if I wanted this behaviour I would have declared this class separately in it's own java file. I know I could always use it as an inner class declaring it inside the public one, but I just don't know why this works this way.

    Can someone explain it to me?

    Thanks in advance, best regards,
    b0rt
    Last edited by b0rt; 08-31-2011 at 08:50 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by b0rt View Post
    Why then only one class can be defined public in one file if at the end all of them can be used outside the file?
    Clarification: if a class does not have an access modifier (and as you have discovered second, third etc classes in the same file cannot have access modifiers) then they have package private access. This means only other classes in the same package have access to that class.
    Last edited by Junky; 08-31-2011 at 08:55 AM.

  3. #3
    b0rt is offline Member
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Default

    Oh, I see...

    It stills feels weird to define "package classes" inside one specific class of the package, but it turns out to be useful, I suppose.

    Thanks a lot Junky!!

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    If you truly need a class to not be accessible by other classes then IMHO it is best to declare it as a private inner class.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I discovered that if I did this in a second java file, the compiler tells me MyClass2 is already defined in that package...
    That's right: what ever classes you define at the "top level" will become part of whatever pacakge the file is part of. SO you can't have two classes with the same name in two different files each part of the same package because that would create two classes with the same name in that package.

    Why then only one class can be defined public in one file if at the end all of them can be used outside the file?
    The one public class per file rule is just Java's way of keeping things neat and tidy. But what exactly do you mean by "can be used"? MyClass1 is defined as having public accessibility while MyClass2 is defined as having default accessibility (what Oracle's Tutorial calls package private). So both can be used by other classes: but in different ways.

    If you want you are completely free to define MyClass2 (with whatever accessibility you want) in its own file. Indeed, if it aids clarity you should do so. But there is no requirement for default access classes to be defined one per file like public ones.
    Last edited by pbrockway2; 08-31-2011 at 09:01 AM. Reason: slow ;(

  6. #6
    b0rt is offline Member
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Default

    Yeah I knew that, I just didn't know I could make "package only" classes this way.

    Actually I'm taking advantage of it, it's just this behaviour felt weird for me due to my ignorance.

    Thanks!!

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by b0rt View Post
    Oh, I see...

    It stills feels weird to define "package classes" inside one specific class of the package, but it turns out to be useful, I suppose.

    Thanks a lot Junky!!
    It does?
    I don't think I've ever seen it used.
    I've always viewed it as a side-effect of the visibility rules rather than anything intended to be functionally useful.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    IMO the only way that's useful is for posting a SSCCE in a forum, and that too only when the 'additional' classes would be better defined in their own files. For other cases, I would use an inner class when creating an SSCCE.

    db

Similar Threads

  1. declaring Classes
    By danderton in forum Java Applets
    Replies: 14
    Last Post: 07-27-2010, 02:29 PM
  2. Declaring a Variable for RGB swaps? Dr Java
    By brandnew956 in forum New To Java
    Replies: 5
    Last Post: 03-03-2010, 02:44 AM
  3. declaring classes
    By coltragon in forum New To Java
    Replies: 17
    Last Post: 12-21-2009, 07:20 PM
  4. Declaring a Queue
    By rhm54 in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 06:02 AM
  5. Declaring Enumeration
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 06:59 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
  •