aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in declaration.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in declaration.." Watch "Doubt in declaration.." New topic
Author

Doubt in declaration..

Dilip Mehra
Greenhorn

Joined: Apr 02, 2012
Posts: 8
When we say :

1) Car c = new Car();// c is refering to the object of Car in the heap

2) Car d; //js declaration it is not refering to anything ,not even null.

3) Car e =null;// now its refering to null(which means nothing).

So where is null stored(in heap?)?

And what exactly is the difference between 1 and 2.Please clear this doubt.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

And what exactly is the difference between 1 and 2.Please clear this doubt.



when you did Car c = new Car(); object intialization takes place. when you did new Car(), memory is allocated on heap and all the instance variables are set to default values. thereafter atleast one constructor is run from the objects class hierarchy. when the car constructor is run, object is fully created(instantiated) and assigned to reference variable c.

when you writer Car d; this is just a declaration. no memory is allocated on heap. if d is an instance variable , it will be given default value for reference variable , which is null upon object creation . if it is a local variable then you must initialize it before you use it.

Null is not a java keyword according to JLS list of keywords. same is true for "true" and "false" .it is a literal but with a special treatment from compiler. you cannot declare an identifier with the name of null, true or false.


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Nice explanation Gurpeet.

gurpeet singh wrote:if d is an instance variable , it will be given default value for reference variable , which is null upon object creation

Just wanted to note that null is assigned not on object creation but on instance reference declaration.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

John Jai wrote:Nice explanation Gurpeet.

gurpeet singh wrote:if d is an instance variable , it will be given default value for reference variable , which is null upon object creation

Just wanted to note that null is assigned not on object creation but on instance reference declaration.




thanks John. But i have read somewhere that the instance members are given default value immediately after memory is allocated for them on the heap(using new operator). thus when we do Car c = new Car(), the first thing that happens is class initalization, in which all the superclasses are initalized. then memory is allocted on the heap. after this instance variables are given default values and instance initalization method (<init> method) corresponding to that constructor is invoked by the jvm, which further calls instance intialization method from superclass constructors. hence first all the superclass instance variables are initialized in order starting from Object class till Car class. i have searched and found the link to really nice tutorial . here is the link http://www.javaworld.com/javaworld/jw-03-1998/jw-03-initialization.html?page=5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

gurpeet singh wrote:
thanks John. But i have read somewhere that the instance members are given default value immediately after memory is allocated for them on the heap(using new operator). thus when we do Car c = new Car(), the first thing that happens is class initalization, in which all the superclasses are initalized. then memory is allocted on the heap. after this instance variables are given default values and instance initalization method (<init> method) corresponding to that constructor is invoked by the jvm, which further calls instance intialization method from superclass constructors. hence first all the superclass instance variables are initialized in order starting from Object class till Car class. i have searched and found the link to really nice tutorial . here is the link http://www.javaworld.com/javaworld/jw-03-1998/jw-03-initialization.html?page=5


The Java Language Specification doesn't actually specify when instance variables get their default values, but yes, we can assume that it is very early in the process, as it is not possible to see an uninitialized instance variable that doesn't have the default value.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in declaration..
 
Similar Threads
Doubt........
Doubt regarding Local array
Doubt
Arrays doubt