• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in declaration..

 
Dilip Mehra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic