• Post Reply Bookmark Topic Watch Topic
  • New Topic

Public Protected and Private Variables  RSS feed

 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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 .
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
K. Tsang, I stand corrected. Thanks!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks again for all your help
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!