This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring members of a non-public class as public

 
Suresh S Nayar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seen code in books where there are members specified as public (and not just default access) in non-public classes. What use could this specification of members as public be in non-public classes be?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless they are also called static and final, they are indications to avoid those books.

Only "constants" should have public access; they are usually declared public static final. Every other field should have private access.
 
Suresh S Nayar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Campbell,

thanks for the reply.

But i don't see the point of Public Constants in non-public classes. If a class is not going to be public, the class won't be accessible, and thus, neither will its public members, right?

And i meant, i have seen member methods declared as public in non-public classes.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh S Nayar wrote:
And i meant, i have seen member methods declared as public in non-public classes.


Well, methods that override public methods in other classes, or that implement interface methods, must be public, even if the class is not. There are many non-public classes that implement interfaces and thus have public methods.
 
Suresh S Nayar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest,

it didn't really strike me earlier that this (overriding public methods from superclasses or implementing an Interface's methods) could be the reason that some members will HAVE to be declared as public, even if a class is not public, effectively restricting the availability of its members to the package's classes and that class's subclasses. Now i am clearer.

cheers
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Restricted to the package and subclasses is protected access.

It is usual for (most) methods to be declared public; they constitute the "public interface" of the class, and ought to be accessible from any code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic