aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes clarfication in null reference object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "clarfication in null reference object" Watch "clarfication in null reference object" New topic

clarfication in null reference object

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
hi in tony morris website

he has written

01 Object a;
02 Object b = null;

On line 1 a reference of type java.lang.Object called 'a' is declared is not assigned (and cannot be used unless it is "definitely assigned" otherwise a compile-time error results).
On line 2 a reference of type java.lang.Object called 'b' is declared and is assigned to the special value null.

in line 1 Object a; will not autmatically assinged null by default
as written in K&B
he is saying compile time error will result ?
why ???

is object a==b ?
if they are instance member ?

is it right that object a will give compile time error if its declared in local block and not as a memeber

Thanks and Regards, Amit Taneja
Terry Rickson

Joined: May 05, 2005
Posts: 22

If you tried using that code in a method it will cause a compiler error because it must be initialised before being used.

If you used it outside the method it automatically gets assigned null (the default value for a Object.

Hope this helps
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Hello amit,
The context of local code is assumed in that example, where a local must be definitely assigned (JLS Chapter 16 - see the link underneath the page) before it can be used.

If the declaration were a field, it would indeed take on the value 'null', since the concept of "definite assignment" is not applicable to fields.

Tony Morris
Java Q&A (FAQ, Trivia)
I agree. Here's the link:
subject: clarfication in null reference object
Similar Threads
Why this output? (in Java 5.0)
Declarations of variable.....
runtime polymorphism ?
my notes on JLS for any1 who needs them !!
Object Referencing