Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what should be the output???  RSS feed

 
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...

 
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.
 
Bartender
Posts: 4044
47
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.
 
Bartender
Posts: 11445
18
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

 
Ranch Hand
Posts: 826
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.....
 
Marshal
Posts: 59110
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch, kshitij kaushik
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!