Originally posted by Ayub ali khan: Could any one clear the following doubt? Can we assign a null reference to a local variable which refers to an interface? ...
Have you tried this? What happened?
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Ayub ali khan
Ranch Hand
Joined: Oct 20, 2005
Posts: 378
2
posted
0
Thank you Ernest and Marc.
I am using Eclipse and it prompts me to initialize the variable which refers to an interface. I have an option to click initialize. When I do that its being initialized to null. I thank Ernest for giving an instant answer to this question (before I could realize the feature of Eclipse).
Sorry for a delayed response.
Thank you for all your assistance. [ February 28, 2006: Message edited by: Ayub ali khan ]
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Learn by Example: First of all, without initializing, you cannot use any kind of Object-Reference; this is not just for Interfaces.
Originally posted by Aum Tao: Isn't the reference initialised automatically to null, if not done explicityly, just like primitive data types?
Only member variables are automatically initialized. Local variables (those defined inside a method or code block) are not.
ak pillai
author
Ranch Hand
Joined: Feb 11, 2006
Posts: 288
posted
0
you cannot use any kind of Object-References without initializing. Instance (aka member) variables are implicitly initialized when the object is created in the heap but the local variables are not in the stack.
Concept:
Each time an object is created in Java it goes into the area of memory known as heap (i.e instance variables also). The primitive variables like int and double are allocated in the stack, if they are local method variables and in the heap if they are member variables (i.e. fields of a class). In Java methods local variables are pushed into stack when a method is invoked and stack pointer is decremented when a method call is completed.