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 Is a Parameter Variable considered a Local Variable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is a Parameter Variable considered a Local Variable?" Watch "Is a Parameter Variable considered a Local Variable?" New topic
Author

Is a Parameter Variable considered a Local Variable?

Perry Terrance
Ranch Hand

Joined: Oct 02, 2009
Posts: 55
I want to be politically-correct on this but it seems Sun defined local variables and parameters as "two" types of Java variables.

However, I always considered method-level parameter variables (that are passed) to be local variables.

Is this politically-correct or should I have just considered them parameters - period?

I know local variables gets destroyed as soon as the method that owns it finishes - so does that mean parameter variables in that method cannot be labeled technically as a local variable?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You're right: parameter variables in a method do work exactly the same way as local variables in that method. Thinking of them as local variables is a good thing to do, too, since they behave exactly like local variables. And you can't have a local variable in a method which has the same name as one of the method's parameters, which pretty much proves that the parameters are in fact local variables.

As for whether you should call them one thing or another, it's best to be clear when you are talking to somebody else. So if you have to mention that you're talking about the parameters of the method, then do so. No point in insisting on a point if it makes something else unclear. I don't recall whether there are official names for those things, but I suppose if I looked in the Java Language Specification I might find out. However it doesn't really matter to me.

(If you're taking an exam where it's important to your score that you use an approved name, that's another thing entirely.)
Perry Terrance
Ranch Hand

Joined: Oct 02, 2009
Posts: 55
Reading Oracle's official Java tutorial here: http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

It states there are four types of variables: instance variables, class variables, local variables, and parameters. This tutorial makes all these variable type seem mutually exclusive (one variable type cannot be another.)

However, reading here:
http://www.ensta-paristech.fr/~diam/java/online/JavaBasics/methods/methods-22-local-variables.html

It states that parameter variables are local variables initialized from argument values.

Which adds to my confusion...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38020
    
  22
I think parameters and local variables are different. They have many behaviours in common, and they occupy similar scope, but there is one big difference: parameters are definitely assigned. It appears that the ex in something like catch (XYZException ex) is also called a parameter.
Perry Terrance
Ranch Hand

Joined: Oct 02, 2009
Posts: 55
So can I say with absolute authority then that according to formal Java specifications - Parameter Variables are a different variable type from Local Variables?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
Perry Terrance wrote:So can I say with absolute authority then that according to formal Java specifications - Parameter Variables are a different variable type from Local Variables?

It would seem so And it seems method parameter variables are a different type to constructor parameter variables.


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is a Parameter Variable considered a Local Variable?
 
Similar Threads
///call by reference///
unfamilier java syntax
answer this
Dout with this key word
Question on Assignments