1

I am trying out the following code from Ivor Horton's Java book in which a two dimensional point is implemented via a simple class.

public class Point
{
  //x and y coordinates
  private xVal,yVal;
  //Constructor
  public Point (double x, double y)
  {
    this.xVal = x;
    this.yVal = y;
  }
  //Constructor
  public Point (final Point aPoint)
  {
    this.xVal = aPoint.xVal;
    this.yVal = aPoint.yVal;

  }

}

Now, the point I don't understand is that in the second constructor which takes an object of type Point as argument, the newly created Point object can access the instance variables x and y of argument Point object directly. This means that private members(methods and variables) of an object can be accessed from inside methods of another object of the same type, in addition to the methods inside the same class. Can anyone please clarify the issue because according to my understanding, the variables of the arugment object should be accessed via getter and setter because they are private.

user1107888
  • 1,481
  • 5
  • 34
  • 55
  • http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private – Sikorski Aug 05 '13 at 10:52
  • 1
    You should usually not modify other instances of `Point` since that's probably not the responsibility of your instance but since any access happens inside some `Point` it is assumed that each instance knows how to handle other instances without breaking encapsulation. You wrote that class after all so you should know what you do. – zapl Aug 05 '13 at 10:56

6 Answers6

3

This is the common misconception that private fields are accessible only by the same instance.

Actually, private fields are private within that class, and not for an instance. So any instance of that class can access private field when in that class.

From JLS - Section 6.6.1:

Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

Emphasis mine.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

It is private to the class, rather than private to the object.

This means other instances of the class can access private variables within an object of the same class.

The docs don't make this immediately obvious - but they do talk about access depending on class, rather than on object :

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Graham Griffiths
  • 2,196
  • 1
  • 12
  • 15
0

First of all there's an error in the code. should be:

public class Point
{
  //x and y coordinates
  private xVal,yVal;
  //Constructor
  public Point (double x, double y)
  {
    this.xVal = x;
    this.yVal = y;
  }
  //Constructor
  public Point (final Point aPoint)
  {
    this.xVal = aPoint.xVal;
    this.yVal = aPoint.yVal;
  }
}

Note the second C'tor aPoint variables.

Secondly, private means private to the Class. Not the instance. So other instances of the class can access the private members/methods.

Assaf Israel
  • 478
  • 3
  • 11
0

From javadocs:

The private modifier specifies that the member can only be accessed in its own class

You an always access private members of the class within the class anywhere including constructors.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

This means that private members(methods and variables) of an object can be accessed from inside methods of another object of the same type

Yes, this is correct. The "private" access modifier works on file level.

This means you can not only access private members from different objects but even from a different class, if one class is a nested (static or non-static), local or anonymous class of the other and if you have a valid object reference of the other class (either explicit or implicit). This works in both directions (outter <-> inner).

Puce
  • 37,247
  • 13
  • 80
  • 152
0

Note that the purpose of access restriction is making explicit the scope and range of the coupling between parts of code.

Seen in this light, private members of a class/instance should be accessible to all code enclosed by the declaration of that class—and indeed they are.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436