• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public inner class

 
Brian Spindler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading SCJP book by Kathy Siera and Bert Bates.

Under source file declaration rules it states:

There can be only ONE public class per source file.

However, this code compiles fine:



Can someone elaborate?
 
Prasad Tamirisa
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well thats a good question,

There can be only ONE public class per source file.


Its correct that there can only be one public class per source file.

The following code will never complie.



Where as, a single public class ( outer) can have a public inner class.
The following code compiles well.



But i am not sure what if the public outer class has 2 public inner classes?


Can any one suggest.
[ May 29, 2007: Message edited by: Prasad Tamirisa ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There can be only one top level public class per source file and the name
of that source file must match the name of the public class.

There can be any number of inner classes per top level class and you can make
all of them public. We make an inner class public to be treated in the same
way members of the class are treated like. BTW, when you need an instance of
the inner class (that is non static) you must have reference to the outer
class.

We can see the public inner class from outside the package also. Whereas
with default access it can be seen inside the package (ofcourse with outer
reference only in any case).



Thanks,
 
Brian Spindler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood, thank you all.
 
Prasad Tamirisa
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks chandra.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic