Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why this code is showing me the default value for double ?


 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or change your constructor to something like:

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Beginning Java.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12122
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Radhakrishna Sharma Gorenta
Ranch Hand
Posts: 63
Google App Engine Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Radhakrishna Sharma Gorenta
Ranch Hand
Posts: 63
Google App Engine Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
I hope Ove Lindström's answer should strike the doubt. Good reply
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic