aspose file tools*
The moose likes Beginning Java and the fly likes why this code is showing me the default value for double ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why this code is showing me the default value for double ?" Watch "why this code is showing me the default value for double ?" New topic
Author

why this code is showing me the default value for double ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

why this code is showing me the default value for double ?




The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575



do heard about shadowing of variable? you are declaring a local variale name as instance variable. now instance variable is masked by local variable in that method. then what you are doing is just assigned 10.45 to localvariable. then you are calling the a in outside of the method. that means you are printing the instance variable.
try this.a = 10.45 instead of a = 10.45 . prefixing this in front of a refers an instance variable!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

or change your constructor to something like:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Seetharaman Venkatasamy wrote:

do heard about shadowing of variable? you are declaring a local variale name as instance variable. now instance variable is masked by local variable in that method. then what you are doing is just assigned 10.45 to localvariable. then you are calling the a in outside of the method. that means you are printing the instance variable.
try this.a = 10.45 instead of a = 10.45 . prefixing this in front of a refers an instance variable!

tried but still the output is 10 and 5. but it should be 50 50 .
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
naved 10 & 5 is what you assign to it. So it is printing 10 & 5. Can you assign it to some other variable in your code to make it print it 50 & 50
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

why should it be 50, 50?

on line 19, you call your test9 constructor. you pass in the values of 50 and 50. inside your method (between lines 5-9, those values are stored in local variables a and b. Note that those are COMPLETELY SEPARATE from your class variables you define on lines 3 and 4.

On line 7, you assign the value of 10 to the current test9 object's a variable (not the local 'a'). you then assign 5 to the object's b variable (not the local 'b'). in fact, your local 'a' and 'b' are never really used. Change your constructor to this:

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

fred rosenberger wrote:why should it be 50, 50?

on line 19, you call your test9 constructor. you pass in the values of 50 and 50. inside your method (between lines 5-9, those values are stored in local variables a and b. Note that those are COMPLETELY SEPARATE from your class variables you define on lines 3 and 4.

On line 7, you assign the value of 10 to the current test9 object's a variable (not the local 'a'). you then assign 5 to the object's b variable (not the local 'b'). in fact, your local 'a' and 'b' are never really used. Change your constructor to this:



why we use this keyword.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Moving to Beginning Java.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
The construct this.x means the x which is a field as opposed to the x which is a local variable or a parameter.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

because you have two different variables called 'a'. you have the variable defined locally to the constructor, and the variable defined as part of the class. inside the constructor, if you don't use the 'this' qualifier, you get the constructor defined one. the ONLY way to get the member variable is to use the 'this' keyword.
rk sharma
Ranch Hand

Joined: Jun 25, 2011
Posts: 50

Hello,
To refer the current class instance variables and methods we use "this" key word. when the local variable and instance variable names are same to differentiate them we use "this" key word.


Regards,

RK
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

naved momin wrote:
tried but still the output is 10 and 5. but it should be 50 50 .


If you don't assign the value to the one that you want to store it in, you have no way of getting it there. Java doesn't do "something magic" with input parameters like you can get i.e. Ada to do.

Take this code:
rk sharma
Ranch Hand

Joined: Jun 25, 2011
Posts: 50

hello,
I hope Ove Lindström's answer should strike the doubt. Good reply
 
jQuery in Action, 2nd edition
 
subject: why this code is showing me the default value for double ?