aspose file tools*
The moose likes Beginning Java and the fly likes Access modifers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access modifers" Watch "Access modifers" New topic
Author

Access modifers

Jayakrishnan.C Chandramohan
Greenhorn

Joined: Aug 08, 2012
Posts: 11
Hi all,


can i declare a class as a private???
like
private class A
{
public static void main(Strings[] g)
{
System.out.println("jk");
}
}
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Jayakrishnan.C Chandramohan wrote:Hi all,


can i declare a class as a private???
like
private class A
{
public static void main(Strings[] g)
{
System.out.println("jk");
}
}


You cannot declare top level classes as private . Inner classes however can be private.

Why do you want to make your class private? If you want the class to be only visible within a package, then you can use package visibility: Just declare the class without an access modifier.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

There are only two access modifiers allowed for classes - public (class can be accessed from anywhere as long as you put import if neccessary) and default (class can be accessed only from the same package).
Hence, your code will result in error and cannot be compiled.

EDIT: import corrected instead of include.


The quieter you are, the more you are able to hear.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38350
    
  23
Kemal Sokolovic wrote:There are only two access modifiers allowed for classes - public (class can be accessed from anywhere as long as you put include if neccessary) and default

. . .
By include, do you mean import? Include is not a Java keyword; are you thinking of C++? As D. Ogranos has already said, inner classes may be private or protected.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Yeah, I was checking some C++ code this morning and made a mistake in a rush, sorry about that.
Original question was directed to classes in general (that's what I understood from it); inner classes are story for itself.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

wouldn't it have been easier to write the code and compile it and see what happens yourself?

You could have gotten an answer in about 1 minute as opposed to the 30+ you had to wait.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jayakrishnan.C Chandramohan
Greenhorn

Joined: Aug 08, 2012
Posts: 11
Thank you allllllllllllllllllll
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access modifers