0

Is there a way in Java to express that an attribute x of an object o can be accessed (I mean by dot notation o.x) only by o itself? To be clear: I'm talking about object-level access as in Smalltalk, not class-level access (thus private is not private enough)?

I'm sorry - I'm sure this has been asked many times before, but I seem to pick the wrong keywords when searching.

steveax
  • 17,527
  • 6
  • 44
  • 59
embee
  • 95
  • 5
  • 2
    No. But what would you need it for? – jlordo Jan 15 '13 at 11:50
  • as long as your variable is **not** `static`, `this.x += 1;` will only affect `x` of the current instance. – jlordo Jan 15 '13 at 11:57
  • Best thing to do is post a short example of code, to demonstrate your _problem_. – jlordo Jan 15 '13 at 11:58
  • I wasn't done with the example yet, just hit RET accidentally. Does it make more sense now? – embee Jan 15 '13 at 11:59
  • 1
    Here it comes again: Well, say, I want each game object to be able to move one step to the right. So in the class definition I write: this.x += 1; But I don't want game objects to manipulate the position of another game object o (of the same class!) by saying: o.x += 1 Every object should be able to change its own position, but not the one of others. Sounds natural to me - and is, in Smalltalk, but not in Java. – embee Jan 15 '13 at 12:02
  • If your object class does not have references of instances of the same class (of other objects) you cannot change them... And I suppose your object class should not have such instances..! – Veger Jan 15 '13 at 12:08
  • Something (some other object) will initiate the move in all objects, right? So essentially will have to communicate it to your moving object somehow. Say, by calling a `moveRight` method: `public void moveRight() { this.x += 1; }`. No? – maksimov Jan 15 '13 at 12:13
  • @Veger: Of course that's a way around what I was looking for ;-) Thing is, the objects could well have access to each other! What I think is a fairly common need is this: I want objects to be able to *read* attributes of others, but let only each object itself *change* them. Coming from a Smalltalk background (I think Ruby's *private* is like Smalltalk, too) Java's *private* strikes me as not private enough. (No flaming at all intended!) – embee Jan 15 '13 at 12:29
  • What you want makes sense. Sadly, there is no way to do it in Java - access is always determined by lexical scope (so, to take another example, an instance of an inner class can see private members of instances of its outer class which are not its own enclosing instance). Fortunately, this isn't that much of a problem in practice. – Tom Anderson Jan 15 '13 at 12:31
  • @Tom: Thanks for confirming my suspicions. (I used to program in Java a lot 10y ago; but have gladly moved on to greener pastures since then, so I'm never quite sure now if I remember my Java correctly.) Your inner class example is instructive, too, thanks. I probably even made use of this 'feature' back in the day, when I was sending around anonymous inner classes (aka closures light) all the time :-) – embee Jan 15 '13 at 12:56

2 Answers2

1

It is not possible in Java (nothing is more private than the fields marked as "private"), but if you think about it, it is also logical: you can modify private fields of other objects only in the common class source code, and if you control the class source code, you could do any bad or good things anyway.

BTW, you can access even private variables of other classes via reflection, if there is no security manager installed, or the policy of the security manager allows it, see this: Why is it allowed to access Java private fields via reflection?

Community
  • 1
  • 1
lbalazscs
  • 17,474
  • 7
  • 42
  • 50
  • Yeah, it can't be helped... Thanks for clarifying. To me, Java access modifiers seem to be most relevant for *library design*. When there is just one programmer with access to everything as in my case (I'm now teaching high school kids and want to show them the benefits of good *encapsulation*) they make almost no sense. Or do they? – embee Jan 15 '13 at 12:45
0

What you want to do is not possible. Every object has its own set of instance variables and can be accessed through that object only as long as they aren't static. So in short, if you want an attribute of Java object accessible by that object only, keep that object alive :).

DarkHorse
  • 2,740
  • 19
  • 28