• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface reference variable

 
Ayub ali khan
Ranch Hand
Posts: 394
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could any one clear the following doubt?

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

Thank you.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

Moving to Java in General (Beginner).
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ayub ali khan
Ranch Hand
Posts: 394
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't the reference initialised automatically to null, if not done explicityly, just like primitive data types?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you have an explicit constuctor that does not initialize all member variables?
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Robert Hill:
Even if you have an explicit constuctor that does not initialize all member variables?


Yes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic