Courtsman
Courtsman

Reputation: 3

Q: In the context provided, is the formal parameter and object considered a local variable?

I'm still learning the basics of CS, and while I know what a formal parameter and object is, I'm wondering if I can ever classify them as local variables.

For example, if I was in a class Foo:

private void bar(int x) { 

   x = x + 5; 
   Foo foo1 = new Foo();
   foo1.doCrazyStuff(); 

} 

Would I still refer to the x's in the method definition as the formal parameter, or would I refer to x as a local variable since its value would disappear once the stack frame goes away? Is it appropriate to consider foo1 a local variable as well?

Upvotes: 0

Views: 38

Answers (2)

wero
wero

Reputation: 33000

Take a look at the various variable types defined by the Java Language Specification:

According to this classification x is a method parameter and foo1 is a local variable.

Upvotes: 1

Thilo
Thilo

Reputation: 262714

Is it appropriate to consider foo1 a local variable as well?

Yes and no.

Parameters are in effect local variables.

But it would be more appropriate to make them final and not use them as such.

Upvotes: 0

Related Questions