Page 1 of 1

About Question enthuware.ocpjp.v7.2.1493 :

Posted: Sat Jul 27, 2013 12:49 pm
by renatumb
Really hard to understand this point!!! :shock:

Could you answer me why does

Code: Select all

public void process(A a)    {       a.i = a.i*2;    }
work/compile fine when I do "i" in A static ?

I think that static members are not inherited( or are they ? :? ), and protected members are accessible in Sub-classes only "through inheritance"... For example, inside B, I could code:

Code: Select all

this.i
super.i
or even:

Code: Select all

new B().i // Accessible in B because protected is to class, but not for the  object
... but... then why I cant do

Code: Select all

new B().i 
inside A ????

Re: About Question enthuware.ocpjp.v7.2.1493 :

Posted: Sat Jul 27, 2013 12:58 pm
by admin
1. Ability to accessing static member through an object reference is just syntactical anomaly of the language. That is how they designed it. It would probably have made more sense if they disallowed it to avoid confusion.

2. Please read 6.6.2. Details on protected Access to understand this.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1493 :

Posted: Thu Aug 01, 2013 4:59 am
by icepeanuts
Another interesting thing is the code works if I put all code in the same package. But the code does not compile when these two classes are in different packages. I do not know why. Can anyone help me out?

Re: About Question enthuware.ocpjp.v7.2.1493 :

Posted: Thu Aug 01, 2013 1:42 pm
by admin
icepeanuts wrote:Another interesting thing is the code works if I put all code in the same package. But the code does not compile when these two classes are in different packages. I do not know why. Can anyone help me out?
That's not too interesting. Classes in the same package can access each others public, protected, and default access fields. This is quite basic.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1493 :

Posted: Sat Aug 03, 2013 1:45 am
by icepeanuts
u r right. thank u.