• Post Reply Bookmark Topic Watch Topic
  • New Topic

how the variable is initialized?  RSS feed

 
Punit Jain
Ranch Hand
Posts: 1085
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i read an article, and one line makes me confuse:

class CoffeeCup {
private int innerCoffee;
//...
}

in the above code the variable innerCoffee, is initialize with the default value 0, but if we explicitly assign this variable say, innerCoffee=10;
it will assign twice, first it's default value and then with 10.
so my question is is it initialize twice???
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:
so my question is is it initialize twice???


No. It is assigned twice, once with the default of 0 and once with our initial value of 10, but only our code is considered initialization.
 
Punit Jain
Ranch Hand
Posts: 1085
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you explain little bit more...?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:can you explain little bit more...?


I don't know what to explain. It's not clear what you don't understand.

First the default values (0/false/null) are assigned by the JVM to all the member variables of the newly created object, then our initializers, like int i = 1; are executed.

All the details are here: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.9
 
Chetan Sarnad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Punit Jain wrote:
so my question is is it initialize twice???


No. It is assigned twice, once with the default of 0 and once with our initial value of 10, but only our code is considered initialization.


The primitive variables will implicitly be assigned the default value after the instantiation if they are not initialized using the constructors as per my understanding.
Do you suggest that irrespective of the initialization, they are first assigned with default values and later replaced or reassigned?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chetan Sarnad wrote:
Jeff Verdegan wrote:
Punit Jain wrote:
so my question is is it initialize twice???


No. It is assigned twice, once with the default of 0 and once with our initial value of 10, but only our code is considered initialization.


The primitive variables will implicitly be assigned the default value after the instantiation if they are not initialized using the constructors as per my understanding.


All variables will get a default value--both primitive and reference--and they will always get it, regardless of whether we initialize them or not.

Do you suggest that irrespective of the initialization, they are first assigned with default values and later replaced or reassigned?


I'm not suggesting it. I'm stating it as an outright fact. You can confirm this with the link I provided above.
 
Punit Jain
Ranch Hand
Posts: 1085
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The primitive variables will implicitly be assigned the default value after the instantiation if they are not initialized using the constructors as per my understanding.


so if i initialize a variable in constructor, it will not be initialized with it's default value???
this time it will initialized only once???
 
Punit Jain
Ranch Hand
Posts: 1085
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

All variables will get a default value--both primitive and reference--and they will always get it, regardless of whether we initialize them or not.


okay, i didn't read the next message, so it will always initialized twice...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:

All variables will get a default value--both primitive and reference--and they will always get it, regardless of whether we initialize them or not.


okay, i didn't read the next message, so it will always initialized twice...


Assigned twice. Not initialized twice.
 
Jon Swanson
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it would help to think of the code this way-



CoffeeCup() is the constructor for the class CoffeeCup. Java will supply this if you don't. Likewise, Java will initialize the instance variables, if you don't. You could write:



and the statement



would create an instance of CoffeeCup with the value of innerCoffee = 10;
 
Chetan Sarnad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Swanson wrote:Maybe it would help to think of the code this way-



CoffeeCup() is the constructor for the class CoffeeCup. Java will supply this if you don't. Likewise, Java will initialize the instance variables, if you don't. You could write:



and the statement



would create an instance of CoffeeCup with the value of innerCoffee = 10;


You are correct. But before being assigned with the value of 10, the variable innerCoffee is assigned with the default value i.e 0 in case of integers. The assignment is done twice.
Right Jeff?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Swanson wrote:before being assigned with the value of 10, the variable innerCoffee is assigned with the default value i.e 0 in case of integers. The assignment is done twice.
Right Jeff?


Correct.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!