aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes public inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "public inner class" Watch "public inner class" New topic
Author

public inner class

Brian Spindler
Greenhorn

Joined: May 17, 2007
Posts: 29
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

Joined: Mar 26, 2007
Posts: 130
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 ]

Regards,
Durga Prasad
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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,


cmbhatt
Brian Spindler
Greenhorn

Joined: May 17, 2007
Posts: 29
Understood, thank you all.
Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
Thanks chandra.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: public inner class
 
Similar Threads
Enum
source file?
What's wrong??
java concepts
source code file