This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof Doubt again?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "instanceof Doubt again??" Watch "instanceof Doubt again??" New topic
Author

instanceof Doubt again??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 8795

Guys,

Consider the code below from Whizlabs,


The question was, On removing which line will the code compile??
The answer was Line 4. I'm confused.


SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What does the compiler error message mean to you?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
ram gaurav
Ranch Hand

Joined: Mar 29, 2006
Posts: 208
It will give compile time error because obj is defined as FINAL and when you already assigned a value to obj in Line 1, then it will give compile time error when you again want to change its value in Line 4
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Jothi,
Once a value to a final reference variable is assigned it can not be changed so //line 4 is invalid.


~Sanjeev Singh<br />SCJP 1.5
Vinayagar Karpagam
Ranch Hand

Joined: Apr 09, 2006
Posts: 72
I believe the key in this question is the Line 2.
Line 4 creates problem because obj is declared as final. A final variable should be assigned a value before the constructor ends.
So, Line 1 can be commented to allow compilation if Line 2 is not there.
That means, Line 2 tries to access an uninitialized final variable (Line 4 assigns value in this case).

Better explanations from other ranchers, please.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 8795

My doubt is, is assigning a value of null also considered??
NDP Prasad
Ranch Hand

Joined: Apr 13, 2006
Posts: 177
If i Comment the Line4 and Run the program, I get the value for Line2 as false.

Iam expecting it should be true.


SCJP,SCWCD<br />
Vinayagar Karpagam
Ranch Hand

Joined: Apr 09, 2006
Posts: 72
Yes Jothi. Even null is a valid final value.

Prasad: (null instanceof Object) == false. Because instanceof operator compares the run-time type of the obj.
Carlos G�mez
Ranch Hand

Joined: Sep 06, 2006
Posts: 56
Jothi

try it:


it will get a compiler error because obj is defined as final and when you already assigned a value to obj in Line 1, you don't reasign a value in line 2.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof Doubt again??
 
Similar Threads
Most specific method.......
Integer[] instanceof Object[]?
Single choice question has multiple answers
Casting, objects, and wrappers
final variable error