About Question enthuware.ocpjp.v7.2.1469 :
Moderator: admin
Re: About Question enthuware.ocpjp.v7.2.1469 :
Don't think this is a point of this question. Here method's parameter of type A can access private member only because the method is declared in the same class as parameter type is. Looks like breaking encapsulation principles a little bit.
-
- Posts: 60
- Joined: Wed Apr 23, 2014 6:38 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1469 :
An attempt to implement the (supposedly faulty) code seems to have worked. I assume I messed up in the implementation, and ask if someone can point out where it went astray. The code below compiles and spits out 20.
Code: Select all
class A
{
private int i;
public void modifyOther(A a1)
{
a1.i = 20; //1
System.out.println(a1.i);
}
public static void main (String[] args) {
A first = new A();
A second = new A();
first.modifyOther(second);
}
}
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1469 :
Not sure what is your question. The code is fine. private means private to the class not to the object. So it is possible for one object to modify other object's private member if they are of the same class.
-
- Posts: 60
- Joined: Wed Apr 23, 2014 6:38 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1469 :
Got it. Thanks for explaining.
-
- Posts: 35
- Joined: Mon Jul 28, 2014 2:05 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1469 :
Although I knew the right answer, as Ryhor I wonder, doesn't that break incapsulation principles?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1469 :
Depends on how you look at it. Java designers certainly accepted it as good enough encapsulationpfilaretov wrote:Although I knew the right answer, as Ryhor I wonder, doesn't that break incapsulation principles?

-Paul.
-
- Posts: 35
- Joined: Mon Jul 28, 2014 2:05 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1469 :
that's funny 

-
- Posts: 42
- Joined: Mon Jan 20, 2014 2:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1469 :
I thought about how does the compiler know whether the object that is being sent to a method is not only a reference type
and that it is a real actual object?
so i assumed that the object of type A should be build first inside the method, and then be accessed in the given manner,
otherwise the accessed variable modifier should have been static.
something like the following for a variable that is not defined static:
And for a variable that is defined static the code as provided in the question might be enough:
But i guess that the compiler does check whether the object being sent to a method is only a reference type or a real object. 
and that it is a real actual object?
so i assumed that the object of type A should be build first inside the method, and then be accessed in the given manner,
otherwise the accessed variable modifier should have been static.
something like the following for a variable that is not defined static:
Code: Select all
class A {
private int i;
publid void modifyOther(A a1) {
a1 = new A() ;
a1.i = 20 ;
}
}
Code: Select all
class A {
private static int i ;
publid void modifyOther(A a1) {
a1.i = 20 ;
}
}

Who is online
Users browsing this forum: Bing [Bot] and 7 guests