File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private member variable confusion

 
Richard Munde
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Munde
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply Marc. I accept that that is how Java is, but it does seem strange to me.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Munde
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never noticed that in C++. Thanks.
 
Richard Munde
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic