Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what should be the output???

 
kshitij kaushik
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

guys the variable "x" i have declared as static and final means I made it constant. so it was expected that the above program should give an error during compiling because in very next statement (go method)to that I am trying to assign a new value to it......but the above program is running and giving output as 4....where is the problem.....and if it is right......then what is the difference between above code and this one...



this is also giving output as 4..
please help me out...

 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shadowing did the trick Kshitij. This feature of Java is called shadowing. You can use the same name for an instance variable and a method parameters. If you take a look at the method

Whatever you pass as a parameter to the method go() is named x and it has nothing to do with the instance variable x. The scope of the method parameter x is only limited to that method. This will throw only an error only if you try to do something like this

So don't get confused with the instance variable x and the method parameter x. The same rule applies for the second piece of code also.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kshitij kaushik wrote:
guys the variable "x" i have declared as static and final means I made it constant. so it was expected that the above program should give an error during compiling because in very next statement (go method)to that I am trying to assign a new value to it......but the above program is running and giving output as 4....where is the problem.....and if it is right......


No. The variable x in the go method is local to the method(though it happens to be the same name with the class variable x,it's just two different scopes). So when you are printing the value inside the method as you are doing, the local variable's value gets printed.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kshitij kaushik wrote:
..I am trying to assign a new value to it......


You are mistaken.
Assignment would have been something like in the go method and you would get a compile error because of the final modifier.
Your final 'x' still carries the value 12. To check this out, in your go method, do the following

 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simple fact that Java is pass-by-value or pass-by-copy.
When you do a=b the bits contained in the variable b is copied into the variable a.

Hope this helps.
 
kshitij kaushik
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot guys......I got mine mistake of understanding.....
 
Campbell Ritchie
Sheriff
Posts: 48640
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch, kshitij kaushik
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic