wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes this #2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "this #2" Watch "this #2" New topic
Author

this #2

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391

I don�t have any questions. I am just trying to confuse someone.
[ April 11, 2003: Message edited by: Marlene Miller ]
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Marlene
I were confused
The answer:
B
2
1
1 ?
1 ?
Please let me know if I answer correct or wrong
thanks


Francis Siu
SCJP, MCDBA
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Bear in mind that fields are statically bound, ie resolved at compile time. So, when t(this)).x is cast to B, the compiler grabs the value of 2. Similarly, the cast to C resolves to 3. Hence, the last three printouts are:
1
2
3


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Roger is right.
Let's see the compile type of the expressions on which the field access axpressions occur. Because we are accessing fields those type will determine at compile time the variable the expression accesses:
this.x
The compile type of this is B. Thus B.x yields 2.
t(this).x
The compile type of t(this) is the one of its return type, that is A. Thus A.x yields 1
(B)t(this).x
The compile type is B, yielding 2
(C)t(this).x
The compile type is C. Thus it yields 3


SCJP2. Please Indent your code using UBB Code
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Jose. You always have such a nice and clear way of explaining. And always JLS-accurate.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Revised...
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
oo.... let me try again
B
2
1
2
3
10
30
oo.... Is it right?
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Yes, this.y in B inherits y from A, so answer is 10.
Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
Hello,
The answer should be:
C // not B
2
1
2
3
10
30
but could anyone tell me how to distinguish compile time and runtime when yeild a field value ?
Thanks!
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
You always get runtime binding for overridden methods. What can be overridden? Methods which are not, either explicitly or implicitly:
- static
- final
- private (implicitly final)
Note that a method of a class which is declared private or final cannot be inherited, let alone overridden.
For anything else, it's compile-time binding.
Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
Thank you very much !
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi everybody
The answer should be:
B
Am I right?
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
It�s C. Would you like to try again?

Here are the clues (in cryptic JLS jargon):
1. When used as a primary expression, the keyword this denotes a value, that is a reference to the object for which the instance method was invoked, or to the object being constructed.
2. The type of "this" is the class X within which the keyword this occurs.
3. At run time, the class of the actual object referred to may be the class X or any subclass of X.
[ April 13, 2003: Message edited by: Marlene Miller ]
Anushkha Rana
Greenhorn

Joined: Apr 13, 2003
Posts: 17
Hello!
The answer should be:
C
C
B
B
Please correct me if i am wrong.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Marlene
thanks for your time to answer
yes this time the answer should be
C
C
----
B
B
thanks for attention
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
And the winners are...Anushka and Siu. CCBB
I am almost done. Only a few more. This next one is tricky.
Quiz #4

The hint, as always, is JLS 15.8.3. That is what I am trying to understand.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Quiz #5. This one is straight-forward. No tricks.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Quiz #6. Another tricky one.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
This is what I think is happening for quiz 6.
The first three printouts will be the runtime type of class C, ie, C, C, C.
But it will fail at runtime on C c = (C)this; when new B().m(); is invoked because you cannot downcast B to C.
Nice quiz.
[ April 14, 2003: Message edited by: Roger Chung-Wee ]
Bryan Helm
Greenhorn

Joined: Apr 07, 2003
Posts: 20
For #5, the first two print statements involve methods, so they will both use the runtime type and print out:
C
C
The real issue is this.x in the third print statement. Since in the class B this is of type B, the hiding of x by C is irrelevant and the printout will be:
1
Badri Sarma
Ranch Hand

Joined: Apr 01, 2003
Posts: 72
Originally posted by siu chung man:
hi Marlene
I were confused
The answer:
B
2
1
1 ?
1 ?
Please let me know if I answer correct or wrong
thanks

B is not correct. The correct answer is C


Thanks<br />Badri
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Marlene
thanks for your time to give more quiz
Quiz #4
Answer:
B
B

Quiz #5
Answer:
Test
C
3

Quiz #6
Answer:
C
B
C
B
B
C

Am I 100% right
Once again
thanks for your time to shire this quiz to us
Anushkha Rana
Greenhorn

Joined: Apr 13, 2003
Posts: 17
Hi All!
This I think should be the answers:
Quiz #4
-------
compiler will complain at line B b = this; because base class reference cannot be assigned to derived class reference without explicit casting. At compile time, keyword this within method m() denotes object of class A.
if its changed to B b = (B)this; then output should be:
B
B
Quiz #5
-------
output should be:
C
C
1
Quiz #6
-------
output should be:
C
C
C
and ClassCastException at C c = (C)this; because base class object cannot be denoted by derived class reference.
Right, Marlene??
[ April 15, 2003: Message edited by: Anushkha Rana ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Quiz 4:
: incompatible types
found : A
required: B
B b = this;
^
1 error
Quiz 5:
C
C
1
Quiz 6:
class C
class C
class C
java.lang.ClassCastException
at A.m(Test13.java:5)
at Test.main(Test13.java:18)
Exception in thread "main"
Thank you Siu, Roger, Jose, Jack, Anushkha, Bryan and Badrinath for sharing in my quest to understand �this�. It was fun.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is what I was trying to understand and feel comfortable with:
�this� has a type known at compile time. The type of �this� is the class within which the keyword �this� occurs. The type of �this� might be different from the type of the object �this� refers to.
Quiz 4: The type of �this� is A. �this� refers to an object of type B.
Quiz 5: The type of �this� is B. �this� refers to an object of type C.
Quiz 6: The type of �this� is A. �this� refers to an object of type C, then an object of type B.
this.getClass() represents the type of the object that �this� refers to.
Thanks again for your interest.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Marlene
I am grateful if you have time to give more quiz,
because I want to know more about "this".
thanks
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Siu, I would be glad to think up some more questions. I like helping people to learn. I'll try to figure out where you get confused. I'll start fresh with a new thread. Marlene
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: this #2