• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

top-level class

 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
who can explain what the top-level class is?
 
Mike Beaty
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A top level class is any class that is not enclosed inside another class. Top-level classes can be public or default.
for example:
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a file, there may be more than 1 top-level class,isn't it?
and cann't be private or protected?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seany,
yes, in a file you can have many top-level classes. Of course, you can only have at most ONE public class, and the file name has to be the same as this public class. So you could have a file full of classes with default access, or many default access classes and ONE public class.
Rob
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, i see.
thank you all!
 
Uday Kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"In a file, there may be more than 1 top-level class,isn't it?
and cann't be private or protected?"
What could be the reason for a top-level class cannot be protected?
[ January 31, 2002: Message edited by: Uday Kumar ]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Uday Kumar:
"In a file, there may be more than 1 top-level class,isn't it?
and cann't be private or protected?"
What could be the reason for a top-level class cannot be protected?
[ January 31, 2002: Message edited by: Uday Kumar ]


Hi Uday
There are couple of arguments I want to put forth, but I am not sure about it, somebody needs to second me over here....(Val please Help!!!)
1) the JLS wants it to be and we have to adhere to it.
2) The Class Loader can only load public or default-access upper level classes. Somebody might argue that inner class are private and the class loader can still load it, but in order to load an inner class, the class loader has to first load the inner-class's upper class. And the class loader treats an inner class as a member of the upper class.
This is my guess........
Bartenders please help!!!
Amish
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Uday Kumar:
"In a file, there may be more than 1 top-level class,isn't it?
and cann't be private or protected?"
What could be the reason for a top-level class cannot be protected?
[ January 31, 2002: Message edited by: Uday Kumar ]


Well, think about it logically.
If you have a top level class marked private, who can use it?
private class foo()
{
int anInt = 12;
}
Private means that only the class can access it. That means that only foo can access itslef. No other class could ever do anything with foo. That doesn't seem very useful does it?
Similarly, with protected, you're saying that classes within the same package can access it. But the default access already covers package access, so in this case supporting protected would be redundant. More serious however, is that if you allowed protected top-level classes, you would have a situation where outside the package, only subclasses of the class could instantiate the class; this would include constructors, so that would effectively prohibit you from instantiating a subclass that was not in the same package. Again, this is redundant behaviour, as the default access already provides for this functionality.
Rob
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob, and I was thinking way hard :roll: ....phew
This makes it much clear!!!
Amish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic