• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Access modifers

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76870
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you allllllllllllllllllll
 
It was the best of times. It was the worst of times. It was a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic