aspose file tools*
The moose likes Beginning Java and the fly likes private member variable confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "private member variable confusion" Watch "private member variable confusion" New topic
Author

private member variable confusion

Richard Munde
Greenhorn

Joined: Feb 18, 2005
Posts: 6
I wrote a little demo of what is causing my confusion, which follows:

public class TwoInts
{
private int num1;
private int num2;

public TwoInts() {
num1 = 0;
num2 = 0;
}

public TwoInts(int n1, int n2) {
num1 = n1;
num2 = n2;
}

public void print() {
System.out.print("num1: " + num1 + " num2: " + num2);
}

public void copy(TwoInts n) {
num1 = n.num1;
num2 = n.num2;
n.num1 = 99;// Shouldn't this be forbidden?
n.num2 = 100;// This too.
}
}

public class TestTwoInts
{
public static void main(String[] args)
{
TwoInts ti1 = new TwoInts();
TwoInts ti2 = new TwoInts(7, 7);

System.out.print("number1: ");
ti1.print();
System.out.print("\n\nti2: ");
ti2.print();
System.out.println("\n");
ti1.copy(ti2);
System.out.println("\nti1 after copy: ");
ti1.print();
System.out.println("\n");
System.out.println("\nti2 after copy: ");
ti2.print();
System.out.println("\n");
}
}

Maybe I'm missing something obvious here, but why am I allowed to change a private variable member in this way? Thanks in advance for your reactions to this.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The modifier "private" restricts access to within the class -- not instances. So different instances of the same class can access each other's private members.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Richard Munde
Greenhorn

Joined: Feb 18, 2005
Posts: 6
Thanks for the quick reply Marc. I accept that that is how Java is, but it does seem strange to me.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Richard Munde:
...it does seem strange to me.

Yeah, it seems strange to a lot of people.

See this thread:
http://www.coderanch.com/t/248423/java-programmer-SCJP/certification/JavaRanch-Rule-Round-up-Game

(I don't remember what I thought of this idea when I was first learning Java. I think I was more confused about "static.")
[ April 08, 2005: Message edited by: marc weber ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Ruby is one of the very few languages that have the kind of "object-based protection" you're imagining. Smalltalk, C++, and (I think, can someone confirm?) Python all do things the same way as Java does.


[Jess in Action][AskingGoodQuestions]
Richard Munde
Greenhorn

Joined: Feb 18, 2005
Posts: 6
I never noticed that in C++. Thanks.
Richard Munde
Greenhorn

Joined: Feb 18, 2005
Posts: 6
Thanks for the link to the explanation Marc. I guess not exposing the existence of private member variables by having to provide public get methods is a point.
Richard Munde
Greenhorn

Joined: Feb 18, 2005
Posts: 6
And let me add that my Java course instructor didn't know the answer to this one. Thank you all very much for your instructive and nearly instantaneous replies. This was my first posting on this board and the experience couldn't have been more positive.
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
to verify Ernest's assumption:

so, yes, Python works like Java in this respect, by default. but that's really not surprising, since Python doesn't have any strictly enforced analog of "private", at all; you can always override a Python class' access restrictions if you really want to.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private member variable confusion