• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what should be the output???

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot guys......I got mine mistake of understanding.....
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch, kshitij kaushik
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic