Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access modifers

 
Jayakrishnan.C Chandramohan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jayakrishnan.C Chandramohan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you allllllllllllllllllll
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic