• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Public Protected and Private Variables

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If public variables are universal and protected only available to the package, classes and methods in it, why wouldn't you use protected say in the driver class instead of public?

I understand that you use private when possible .
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Kellytt wrote:If public variables are universal and protected only available to the package, classes and methods in it, why wouldn't you use protected say in the driver class instead of public?

I understand that you use private when possible .



Assuming you mean JDBC driver class. That depends on where you calling your getConnection() method in the same package or in another package.

In real world apps, usually there are several packages.
 
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I understand it, protected members are only accesible by the class itself and its subclasses, not the whole package.
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles D. Ward wrote:As I understand it, protected members are only accesible by the class itself and its subclasses, not the whole package.



Charles, the protected access modifier is for class, package and subclasses.

From the Java tutorials on access modifiers http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K. Tsang, I stand corrected. Thanks!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by the "driver class"?

You should make member variables private, unless there is a good reason to give them any other access specifier.
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Jesper, the Driver or testing class is the class that contains main.

Many thanks to everyone for your advice. The code I am writing at the moment only has one package so that explains it perfectly.

Should I forget protected altogether then?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Kellytt wrote:Sorry Jesper, the Driver or testing class is the class that contains main.


Ah, OK. Well in that case the answer is: because you don't need to. The only thing that your definition of a "driver" class should contain is a main() method with either one or two lines in it. No other methods, and certainly no variables.

For a couple of patterns, have a look at the MainIsAPain page.

Should I forget protected altogether then?


No, but you should only use it when there's a real need - ie, when you want something visible to subclasses (the package visibility is a bit of a red herring, since you normally use protected for subclass visibility).

And, tempting as it may seem to use it on variables, I generally only use it with methods. If I need a variable to only be visible to subclasses, I'll usually provide a protected getter (and possibly a setter too; but ONLY if it's needed).

HIH

Winston
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles D. Ward wrote:As I understand it, protected members are only accesible by the class itself and its subclasses, not the whole package.

A lot of people think that; C++ uses protected to mean that. But it is different in Java.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:A lot of people think that; C++ uses protected to mean that. But it is different in Java.


I have to admit to a preference for C++'s interpretation, but TBH it doesn't make that much difference.

Winston
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again for all your help
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . I have to admit to a preference for C++'s interpretation, . . .

Actually, me too. There used to be something called private protected in Java™, which did exactly that, but it was withdrawn about the time of JDK1.1

I have seen people get into no end of confusion because Java and C/C++ interpret the same keyword to mean something different.
reply
    Bookmark Topic Watch Topic
  • New Topic