• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interface reference variable

 
Ranch Hand
Posts: 399
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.

Moving to Java in General (Beginner).
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 399
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if you have an explicit constuctor that does not initialize all member variables?
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic