Home arrow static arrow Java Programming [Archive] - Private class? Is this Possible?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Private class? Is this Possible?
This topic has 21 replies on 2 pages.    1 | 2 | Next »

Posts:754
Registered: 6/13/03
Private class? Is this Possible?  
Jul 19, 2004 5:49 AM



 
Hi ya,

Can a class be defined as 'private'?
If yes,what is the advantage of this and who can
call this class?
 

Posts:24,036
Registered: 2/3/03
Re: Private class? Is this Possible?  
Jul 19, 2004 5:51 AM (reply 1 of 21)



 
Hi ya,

Can a class be defined as 'private'?

Yes, inner/nested classes may be private; top-level classes may not.

If yes,what is the advantage of this

Encapsulation.

and who can
call this class?

The enclosing class.
 

Posts:4,906
Registered: 23/07/02
Re: Private class? Is this Possible?  
Jul 19, 2004 5:56 AM (reply 2 of 21)



 
Correction: any class in the same compilation unit ( = file on most OSes) can access a private class. Also, they can be top-level, but only if you have multiple top-level classes in a compilation unit, which most people would strongly discourage now that we have inner classes.
 

Posts:24,036
Registered: 2/3/03
Re: Private class? Is this Possible?  
Jul 19, 2004 6:02 AM (reply 3 of 21)



 
Correction: any class in the same compilation unit ( =
file on most OSes) can access a private class. Also,
they can be top-level, but only if you have multiple
top-level classes in a compilation unit, which most
people would strongly discourage now that we have
inner classes.

// Foo.javapublic class Foo {} private class Bar {}

I may be misunderstanding what you're saying, but this example will not compile. Top-level classes may only have public or default access modifiers.
 

Posts:24,036
Registered: 2/3/03
Re: Private class? Is this Possible?  
Jul 19, 2004 6:05 AM (reply 4 of 21)



 
Furthermore, only the enclosing class can access private members.
public class Foo {     private class Bar {}} class Baz {    Bar b = new Bar(); // Cannot resolve symbol}

If I'm missing something in YAT's post, let me know! :o)
 

Posts:569
Registered: 8/8/01
Re: Private class? Is this Possible?  
Jul 19, 2004 6:10 AM (reply 5 of 21)



 
Everything is fine.
You're right.

But, I think, older JDKs support the compilation of private and public classes in one file.
 

Posts:1,860
Registered: 12/14/00
Re: Private class? Is this Possible?  
Jul 19, 2004 6:13 AM (reply 6 of 21)



 
If I'm missing something in YAT's post, let me know!
:o)

Nope, YAT did indeed get this one wrong. Sections 8.1.1 and 6.6.1 of the JLS provide the evidence.
 

Posts:24,036
Registered: 2/3/03
Re: Private class? Is this Possible?  
Jul 19, 2004 6:14 AM (reply 7 of 21)



 
Everything is fine.

Well, thank goodness for that. My world-view was about to shatter into a million tiny fragments. ;o)
 

Posts:1,083
Registered: 6/12/03
Re: Private class? Is this Possible?  
Jul 19, 2004 7:31 AM (reply 8 of 21)



 
I've seen examples of Singleton classes that use a private constructor.
 

Posts:1,083
Registered: 6/12/03
Re: Private class? Is this Possible?  
Jul 19, 2004 7:32 AM (reply 9 of 21)



 
OOps (sorry for the pun..) You're discussing private "classes"...Never mind...
 

Posts:319
Registered: 11/8/00
Re: Private class? Is this Possible?  
Jul 19, 2004 7:34 AM (reply 10 of 21)



 
classes with only private constructors implicitly effectively final.

Not really sure why that relevent but i'm bored!
 

Posts:4,906
Registered: 23/07/02
Re: Private class? Is this Possible?  
Jul 19, 2004 9:21 AM (reply 11 of 21)



 
Nope, YAT did indeed get this one wrong. Sections 8.1.1 and 6.6.1 of the JLS provide the evidence.

I apologise. Has this changed, though? I'm sure compilation units were relevant once, but now the JLS appears to have virtually nothing to say about them.
 

Posts:24,036
Registered: 2/3/03
Re: Private class? Is this Possible?  
Jul 19, 2004 9:26 AM (reply 12 of 21)



 
Nope, YAT did indeed get this one wrong. Sections
8.1.1 and 6.6.1 of the JLS provide the evidence.

I apologise. Has this changed, though? I'm sure
compilation units were relevant once, but now the JLS
appears to have virtually nothing to say about them.

I've only been playing since 1.3, so prior to that I don't know. But, it's been this way since 1.3, at least.
 

Posts:1,860
Registered: 12/14/00
Re: Private class? Is this Possible?  
Jul 19, 2004 9:36 AM (reply 13 of 21)



 
Has this changed, though? I'm sure
compilation units were relevant once, but now the JLS
appears to have virtually nothing to say about them.

Under the first edition of the JLS, private was not a valid class modifier at all, so you certainly couldn't have private classes in the same compilation unit as public ones back then. There were no nested classes back then either, so the issue as to whether classes in the same compilation unit could access them is moot.

Bear in mind that only allowing a single public class in any one compilation unit is a limit of javac (and other popular compilers), not of Java in general.
 

Posts:6,750
Registered: 1/25/04
Re: Private class? Is this Possible?  
Jul 19, 2004 9:58 AM (reply 14 of 21)



 
"When packages are stored in a file system (�7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:

The type is referred to by code in other compilation units of the package in which the type is declared.
The type is declared public (and therefore is potentially accessible from code in other packages).

This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.

When packages are stored in a database (�7.2.2), the host system must not impose such restrictions."
 
This topic has 21 replies on 2 pages.    1 | 2 | Next »