Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Strange Private Variables - Why does this work?

Reply
Thread Tools

Strange Private Variables - Why does this work?

 
 
CJ
Guest
Posts: n/a
 
      09-29-2003
Hi,

Came across this issue - anyone know why this works?

public class Cat {
private int privateVal;
public void test()
{
Cat c1 = new Cat();
c1.privateVal = 1;
}
}

Surely the "privateVal" variable should not be accessible at this
point (should be out of scope)? Anyone care to enlighten me??

Thanks

CJ
 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      09-29-2003

"CJ" <> wrote in message
news: om...
> Hi,
>
> Came across this issue - anyone know why this works?
>
> public class Cat {
> private int privateVal;
> public void test()
> {
> Cat c1 = new Cat();
> c1.privateVal = 1;
> }
> }
>
> Surely the "privateVal" variable should not be accessible at this
> point (should be out of scope)? Anyone care to enlighten me??
>


private members are available to *any* code in the same class

--
Mike W


 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      09-29-2003
CJ <> scribbled the following:
> Hi,


> Came across this issue - anyone know why this works?


> public class Cat {
> private int privateVal;
> public void test()
> {
> Cat c1 = new Cat();
> c1.privateVal = 1;
> }
> }


> Surely the "privateVal" variable should not be accessible at this
> point (should be out of scope)? Anyone care to enlighten me??


> Thanks


It's a bug/feature (choose one) in Java that objects in the same class
can access each other's private variables.
It's not a scope issue but a visibility issue. IMHO it breaks the
"encapsulation" idea but it can solve a lot of practical problems.

--
/-- Joona Palaste () ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners."
- Ernst Jan Plugge
 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      09-29-2003

"CJ" <> wrote in message
news: om...
> Hi,
>
> Came across this issue - anyone know why this works?
>
> public class Cat {
> private int privateVal;
> public void test()
> {
> Cat c1 = new Cat();
> c1.privateVal = 1;
> }
> }
>
> Surely the "privateVal" variable should not be accessible at this
> point (should be out of scope)? Anyone care to enlighten me??
> CJ


And it's not a 'scope' issue, it's a 'visibilty' one.
--
Mike W


 
Reply With Quote
 
Phillip Lord
Guest
Posts: n/a
 
      09-29-2003
>>>>> "Joona" == Joona I Palaste <> writes:

Joona> CJ <> scribbled the following:
>> Hi,


>> Came across this issue - anyone know why this works?


>> public class Cat { private int privateVal; public void test() {
>> Cat c1 = new Cat(); c1.privateVal = 1; } }


>> Surely the "privateVal" variable should not be accessible at this
>> point (should be out of scope)? Anyone care to enlighten me??


>> Thanks


Joona> It's a bug/feature (choose one) in Java that objects in the
Joona> same class can access each other's private variables. It's
Joona> not a scope issue but a visibility issue. IMHO it breaks the
Joona> "encapsulation" idea but it can solve a lot of practical
Joona> problems.


Just depends how you look at it. Variables are private to the class,
and not the object. In this case you are calling the private variable
from the same class.

It makes sense to me. The point of privacy is to say "you should only
be changing this if you know what you are doing". If you have
implemented Cat, then you should know what you are doing with all
objects of Cat.

Phil
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      09-30-2003
Phillip Lord <> scribbled the following:
>>>>>> "Joona" == Joona I Palaste <> writes:

> Joona> CJ <> scribbled the following:
> >> Hi,


> >> Came across this issue - anyone know why this works?


> >> public class Cat { private int privateVal; public void test() {
> >> Cat c1 = new Cat(); c1.privateVal = 1; } }


> >> Surely the "privateVal" variable should not be accessible at this
> >> point (should be out of scope)? Anyone care to enlighten me??


> >> Thanks


> Joona> It's a bug/feature (choose one) in Java that objects in the
> Joona> same class can access each other's private variables. It's
> Joona> not a scope issue but a visibility issue. IMHO it breaks the
> Joona> "encapsulation" idea but it can solve a lot of practical
> Joona> problems.


> Just depends how you look at it. Variables are private to the class,
> and not the object. In this case you are calling the private variable
> from the same class.


I would have to disagree with the notion "variables are private to the
class, and not the object". If they're not private to the object, then
how come each object gets its own copy of them, and can alter them to
its heart's content without disturbing the other objects of the same
class? Huh?

> It makes sense to me. The point of privacy is to say "you should only
> be changing this if you know what you are doing". If you have
> implemented Cat, then you should know what you are doing with all
> objects of Cat.


I'd take that point of privacy one level further, and allow one single
*object* to only modify itself willy-nilly. After all, objects are a
more usable concept than classes, which can be shown by the existence
of languages with objects but not classes, whereas I don't know any
with classes but not objects.

--
/-- Joona Palaste () ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I am lying."
- Anon
 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      09-30-2003
Joona I Palaste <> writes:

> I would have to disagree with the notion "variables are private to the
> class, and not the object". If they're not private to the object, then
> how come each object gets its own copy of them, and can alter them to
> its heart's content without disturbing the other objects of the same
> class? Huh?


Because you're confusing two totally different meanings of "private"
here. There is private as in "uniquely belongs to" (your use) and
private as in "the Java access modifier" (the use under duscussion).
In the latter case, a private member of an instance of a class is
accessibe to *any* code in that same class that can get at that
instance.

 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      10-01-2003
Tor Iver Wilhelmsen <> scribbled the following:
> Joona I Palaste <> writes:
>> I would have to disagree with the notion "variables are private to the
>> class, and not the object". If they're not private to the object, then
>> how come each object gets its own copy of them, and can alter them to
>> its heart's content without disturbing the other objects of the same
>> class? Huh?


> Because you're confusing two totally different meanings of "private"
> here. There is private as in "uniquely belongs to" (your use) and
> private as in "the Java access modifier" (the use under duscussion).
> In the latter case, a private member of an instance of a class is
> accessibe to *any* code in that same class that can get at that
> instance.


So let me get this straight, Java provides no way of having private
as in "uniquely belongs to"? In that case Java is IMHO breaking one
principle of "good" OO. Another such breach is the existence of non-
polymorphic primitives. It could also be argued that static variables
and methods are also breaches against "good" OO.
However, no one ever claimed Java was a *pure* OO language.

--
/-- Joona Palaste () ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You have moved your mouse, for these changes to take effect you must shut down
and restart your computer. Do you want to restart your computer now?"
- Karri Kalpio
 
Reply With Quote
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      10-01-2003
Joona I Palaste wrote:

> Tor Iver Wilhelmsen <> scribbled the
> following:
>> Joona I Palaste <> writes:
>>> I would have to disagree with the notion "variables are private to the
>>> class, and not the object". If they're not private to the object, then
>>> how come each object gets its own copy of them, and can alter them to
>>> its heart's content without disturbing the other objects of the same
>>> class? Huh?

>
>> Because you're confusing two totally different meanings of "private"
>> here. There is private as in "uniquely belongs to" (your use) and
>> private as in "the Java access modifier" (the use under duscussion).
>> In the latter case, a private member of an instance of a class is
>> accessibe to *any* code in that same class that can get at that
>> instance.

>
> So let me get this straight, Java provides no way of having private
> as in "uniquely belongs to"? In that case Java is IMHO breaking one
> principle of "good" OO. Another such breach is the existence of non-
> polymorphic primitives. It could also be argued that static variables
> and methods are also breaches against "good" OO.
> However, no one ever claimed Java was a *pure* OO language.
>


Is that really such a problem in real life?
Seriously, if a programmer is designing and coding the class, he sure knows
(or should know what he's doing with any instance of the class.
Also, how would you start to implement methods like equals/compareTo if you
can't reach the other object's private fields? Objects would only be able
to be compared on public properties, wich might not work in some
conditions.

--
mvg,
Christophe Vanfleteren
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      10-01-2003
Christophe Vanfleteren <> scribbled the following:
> Joona I Palaste wrote:


(snip)

>> So let me get this straight, Java provides no way of having private
>> as in "uniquely belongs to"? In that case Java is IMHO breaking one
>> principle of "good" OO. Another such breach is the existence of non-
>> polymorphic primitives. It could also be argued that static variables
>> and methods are also breaches against "good" OO.
>> However, no one ever claimed Java was a *pure* OO language.


> Is that really such a problem in real life?
> Seriously, if a programmer is designing and coding the class, he sure knows
> (or should know what he's doing with any instance of the class.
> Also, how would you start to implement methods like equals/compareTo if you
> can't reach the other object's private fields? Objects would only be able
> to be compared on public properties, wich might not work in some
> conditions.


Here's a solution. Divide the "private" visibility modifier into two
new ones: "really private" and "soft-of private" (they need better
names). A field or method that is "really private" is only ever visible
to the object it belongs to. A field or method that is "sort-of
private" is only ever visible to objects of the same class.
Fields that the equals and compareTo methods need would be "sort-of
private". Everything else (that used to be private) would be "really
private".

--
/-- Joona Palaste () ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"We're women. We've got double standards to live up to."
- Ally McBeal
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing a private function an keeping access to private variables Gregor Kofler Javascript 6 06-27-2008 10:24 PM
private(true) || private :make_time, what does it do? why? Rie! Ruby 3 03-26-2008 12:40 PM
Why is define_method private? Plus,what's the point of private methods? Daniel Finnie Ruby 3 12-16-2006 10:09 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
RE: Why I use private variables (WAS: RE:"private" variablesa.k.a. name mangling?) Jeremy Bowers Python 3 01-24-2005 10:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57