• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

private member variable confusion

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
https://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 ]
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never noticed that in C++. Thanks.
 
Richard Munde
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Everybody's invited. Even this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic