Results 1 to 8 of 8
- 08-31-2011, 07:48 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 22
- Rep Power
- 0
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:
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 MyClass1.java package myclasses; public class MyClass1{ //class stuff } class MyClass2{ //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.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 }
Can someone explain it to me?
Thanks in advance, best regards,
b0rtLast edited by b0rt; 08-31-2011 at 07:50 AM.
- 08-31-2011, 07:52 AM #2
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 07:55 AM.
- 08-31-2011, 07:56 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 22
- Rep Power
- 0
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!!
- 08-31-2011, 07:59 AM #4
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.
- 08-31-2011, 08:01 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.I discovered that if I did this in a second java file, the compiler tells me MyClass2 is already defined in that package...
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.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?
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 08:01 AM. Reason: slow ;(
- 08-31-2011, 08:02 AM #6
Member
- Join Date
- Aug 2011
- Posts
- 22
- Rep Power
- 0
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!!
- 08-31-2011, 09:40 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 08-31-2011, 10:38 AM #8
Similar Threads
-
declaring Classes
By danderton in forum Java AppletsReplies: 14Last Post: 07-27-2010, 01:29 PM -
Declaring a Variable for RGB swaps? Dr Java
By brandnew956 in forum New To JavaReplies: 5Last Post: 03-03-2010, 01:44 AM -
declaring classes
By coltragon in forum New To JavaReplies: 17Last Post: 12-21-2009, 06:20 PM -
Declaring a Queue
By rhm54 in forum New To JavaReplies: 1Last Post: 03-21-2008, 05:02 AM -
Declaring Enumeration
By Java Tip in forum Java TipReplies: 0Last Post: 11-04-2007, 05:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks