Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Reference to Null

 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have very small doubt.

Integer intvar;

while(...){
intvar=it.next(); //Here Error comes,Var->Not initialized
}

but if
i do like Integer intvar=null;
It works .Althought i am not 'new'ing the intvar objectRef.
But how it works with null ,which also means pointing to nothing

Thank you
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're declaring intvar as a local object within a method.

A local variable must be initialized before using. If not you will have a compile time error which states that the variable is not initialized.

In your second attempt you initialize your object to null. So it's initialized.
 
kirba devi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Remko,
Local variable must be initialized before using.
the statement what you said is correct....
by looking at the code

Integer intvar;

while(...){
intvar=it.next(); //Here Error comes,Var->Not initialized
}

dont consider intvar as a local object."See where it is declared".it
is an instance reference variable(for the object goi to create) then it will automatically get initialized to null..

may be some wrong withe the coding intvar=it.next.....
if he typed the code like

while(..){
Integer intvar..

then it is a local variable...

if i am wrong with the above statements please let me know...

Thanks
Kirba.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the example you showed should compile just fine (except for the syntax errors). Look at this:
This example compiles without error as shown, however if you uncomment the last line you will get a compile time error:
What's happening is that you can't use a reference until its been definitely assigned, the compiler cannot guarantee that the while loop will ever execute, therefore it cannot guarantee that 'var' will be definitely assigned.
[ June 30, 2007: Message edited by: Garrett Rowe ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to throw another log on this fire, this compiles without error:
because the compiler can infer that the loop will definitely execute at least once.
[ June 30, 2007: Message edited by: Garrett Rowe ]
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I may not know the right way to say it, but, I have noticed in several texts that, even though in the order of your method's sequence you ARE setting the variable before it is used, IF it is down in nested logic, it can STILL throw the compiler and cause a warning to be generated. Those books say that the correct thing to do in these cases is to initialize the variable to some reasonable value when it is declared.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic