File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes what should be the output??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what should be the output???" Watch "what should be the output???" New topic
Author

what should be the output???

kshitij kaushik
Greenhorn

Joined: Jan 14, 2009
Posts: 11

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



thanx
kshitij
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

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.

SCJP 5.0 and now fighting with ExtJS
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3848

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.

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

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.


Regards, Sud.
SCJP 5 ScjpFAQ JLS
kshitij kaushik
Greenhorn

Joined: Jan 14, 2009
Posts: 11
thanks a lot guys......I got mine mistake of understanding.....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
And welcome to JavaRanch, kshitij kaushik
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what should be the output???