aspose file tools*
The moose likes Beginning Java and the fly likes interface reference variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "interface reference variable" Watch "interface reference variable" New topic
Author

interface reference variable

Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 382
    
    2
Could any one clear the following doubt?

Can we assign a null reference to a local variable which refers to an interface?

Thank you.


SCEA part I,TOGAF Foundation
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Yes.

Moving to Java in General (Beginner).


[Jess in Action][AskingGoodQuestions]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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: 382
    
    2
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
Learn by Example:
First of all, without initializing, you cannot use any kind of Object-Reference; this is not just for Interfaces.

Example:
java.io.Serializable is an interface


not;
but this will compile and work:
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
Isn't the reference initialised automatically to null, if not done explicityly, just like primitive data types?


SCJP 1.4 85%
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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
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.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
Even if you have an explicit constuctor that does not initialize all member variables?
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Robert Hill:
Even if you have an explicit constuctor that does not initialize all member variables?


Yes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface reference variable