The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Initialize your variable before using it...

Posted by Kishori Sharan on January 05, 2001 at 9:26 AM

Your program cannot compile because using algorithm of definitive assignment Java makes sure at compile time that each variable which is being used has been initialized ( I mean has a value. Note that I am saying each variable used. It means you can declare a variable but when you try to use its value then only it is a problem if you haven't assigned any value to it or it has not been implicitly assigned a value as in case of instance variable. ) . You just declared a reference variable "a" of type one as in
one a ;
but you didn't put any reference in "a" before using it in th.display(a). You need to do something like
one a = new two ( ) ;
one a = new three ( ) ;
and then it will work.

If you don;t want to do that then just to compile your program you can just declare one a ; as the instance variable of class three ( here you will have to declare static one a ; because you are using it in main ( ) method which is static ). Becuase each reference instance/class variables are initialized to null by default your program will compile but in display method it will throw NullPOinterException.

static void main(String args[]){
three th= new three();
one a;


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us