About Question enthuware.ocpjp.v7.2.1493 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
renatumb
Posts: 47
Joined: Mon Apr 08, 2013 7:55 pm
Contact:

About Question enthuware.ocpjp.v7.2.1493 :

Post 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 ????

admin
Site Admin
Posts: 10067
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10067
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

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

Post by icepeanuts »

u r right. thank u.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 238 guests