This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Access modifers

 
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");
}
}
 
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.
 
Bartender
Posts: 825
5
Python Ruby Java
  • 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.
 
Marshal
Posts: 70263
282
  • 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
Python Ruby Java
  • 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.
 
lowercase baba
Posts: 12893
63
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
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic