• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clarfication in null reference object

 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi in tony morris website

he has written

that
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
 
Terry Rickson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic