this #2
posted 22 years ago
I don�t have any questions. I am just trying to confuse someone.
[ April 11, 2003: Message edited by: Marlene Miller ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I don�t have any questions. I am just trying to confuse someone.
[ April 11, 2003: Message edited by: Marlene Miller ]
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
hi Marlene
I were confused
The answer:
B
2
1
1 ?
1 ?
Please let me know if I answer correct or wrong
thanks

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
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
1
2
3
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
Posts: 1392
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thank you Jose. You always have such a nice and clear way of explaining. And always JLS-accurate.
Marlene Miller
Ranch Hand
Posts: 1392
Francis Siu
Ranch Hand
Posts: 867
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
oo.... let me try again
B
2
1
2
3
10
30
oo.... Is it right?
B
2
1
2
3
10
30
oo.... Is it right?
Francis Siu
SCJP, MCDBA
Roger Chung-Wee
Ranch Hand
Posts: 1683
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Yes, this.y in B inherits y from A, so answer is 10.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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!
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
Posts: 1683
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
- 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.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Jack Lau
Ranch Hand
Posts: 168
Francis Siu
Ranch Hand
Posts: 867
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
hi everybody
The answer should be:
B
Am I right?
The answer should be:
B
Am I right?

Francis Siu
SCJP, MCDBA
Marlene Miller
Ranch Hand
Posts: 1392
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
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 ]
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hello!
The answer should be:
C
C
B
B
Please correct me if i am wrong.
The answer should be:
C
C
B
B
Please correct me if i am wrong.
Francis Siu
Ranch Hand
Posts: 867
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
hi Marlene
thanks for your time to answer
yes this time the answer should be
C
C
----
B
B
thanks for attention
thanks for your time to answer
yes this time the answer should be
C
C
----
B
B
thanks for attention

Francis Siu
SCJP, MCDBA
Marlene Miller
Ranch Hand
Posts: 1392
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
Posts: 1392
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Quiz #5. This one is straight-forward. No tricks.
Marlene Miller
Ranch Hand
Posts: 1392
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Quiz #6. Another tricky one.
Roger Chung-Wee
Ranch Hand
Posts: 1683
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
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 ]
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 22 years ago
B is not correct. The correct answer is C
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
Posts: 867
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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

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

Francis Siu
SCJP, MCDBA
Anushkha Rana
Greenhorn
Posts: 17
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
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
Posts: 1392
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
: 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
Posts: 1392
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
�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
Posts: 867
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
hi Marlene
I am grateful if you have time to give more quiz,
because I want to know more about "this".
thanks

I am grateful if you have time to give more quiz,
because I want to know more about "this".
thanks

Francis Siu
SCJP, MCDBA
Marlene Miller
Ranch Hand
Posts: 1392
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
| Something must be done about this. Let's start by reading this tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |






