• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Local variable vs. instance variable

 
Ranch Hand
Posts: 74
2
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following code:



Prints:

42
16



But if I change the nonaccess modifier in int size = 42; to final int size = 42; I'll get an exception:

42
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The final field TestFin.size cannot be assigned

at TestFin.changeSize(TestFin.java:8)
at TestFin.main(TestFin.java:12)

But size in the method changeSize() is a local variable and size in the class TestFin is an instance variable? That's why it's possible to print both variables size in the code without final: 42 and 16.

Why has final int size = 42; something to do with the local variable size = 16;? I've learned that local and instance variable are not the same?! And when it's not the same it should be possible to give size another value int the method (with final)! But why it doesn't work?

Thanks.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Gualeni wrote:... But size in the method changeSize() is a local variable...


No, it isn't. Line 6 doesn't declare a new, local variable size, but instead assigns a new value to the instance variable size. Can you tell why? What would you need to do to actually declare a new local variable size on line 6?

That's why it's possible to print both variables size in the code without final: 42 and 16.


As I hinted above, there's just one variable. Can you figure out why the code prints two different values, even though there is just one variable?


By the way, this is exactly the reason to avoid using local variables that have the same name as instance variables.
 
Mike Gualeni
Ranch Hand
Posts: 74
2
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Thanks for your answer.

But I've no clue, how I can to do to actually declare a new local variable size on line 6? And why it prints two variables, even though there is just one variable? Has this something to do with shadowing or 'Variable Hiding'? But in the method changeSize() is another scope than in the class?!

I've tried this code:



It prints "18". Is this a correct solution? this.size on the left side is now the new instance variable value assigned from the right side this.size = size +2; = 18. The instance variable value has changed from 42 to 18, right?
But what I have to do if i want print the instance variable + 2? this.size should be the instance variable? But the ouput is the same in this code:


Thanks for clarification.

 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Gualeni wrote:. . . how I can to do to actually declare a new local variable . . .

Same way you declare any other variables: modifiers, type and name. You may find that final is the only modifier you can use for a local variable. Line 6 hasn't got a type, so I shall let you work how many occurrences of size in that method refer to the field and how many to something else.

And why it prints two variables, even though there is just one variable?

It doesn't actually print two variables, but one variable and one returned value from a method call.

Has this something to do with shadowing or 'Variable Hiding'?

You only see shadowing if you have a local variable and you only see hiding when you have a subclass.

But in the method changeSize() is another scope than in the class? . . .

What does that mean? You don't have any local variables to have a different scope. The only local variables you have are in the main() method.

It prints "18". Is this a correct solution? . . .

The program calculates 18, yes. And because you don't have any local variables, the two programs are equivalent to each other.
 
Mike Gualeni
Ranch Hand
Posts: 74
2
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why in line 7 this.size corresponds not to the instance variable int size = 42;? I thought this is always the actual object of the class? If I have a method doStuff(int z) with the argument int z and I want to assign z to the same instance variable in the class I write this.z = z; too?! But then this corresponds with the class instance variable. But why not in my code example? Why in my 2 code examples this.size changes nothing? Why corresponds this.size with size = 16; and not int size = 42;? By the way, how can I access the instance variable in the method changeSize()?
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Gualeni wrote:But why in line 7 this.size corresponds not to the instance variable int size = 42;?


The this.size on line 7 refers to the instance variable size. There isn't any other variable named size than the instance variable declared on line 3. (Have you read my earlier reply?)

I thought this is always the actual object of the class?


This is true.

If I have a method doStuff(int z) with the argument int z and I want to assign z to the same instance variable in the class I write this.z = z; too?! But then this corresponds with the class instance variable.


In this case, there's an instance variable z and a local variable z; the local variable is declared in the method parameter list (the "int z" in doStuff(int z)) - a method parameter is essentially a local variable initialized to a value passed to the method by the caller.

Why in my 2 code examples this.size changes nothing?


This isn't true. The statement this.size = size +2; in you code changes the value of the instance variable size. To see it does, just add another statement System.out.println(tf.size); after line 13.

Why corresponds this.size with size = 16; and not int size = 42;?


There's only one variable named size, and both size and this.size refer to this one variable. The variable is initialized to 42 on line 3, then printed out on line 12, then changed to 16 on line 6, then changed to 18 on line 7, then returned to the caller and printed out on line 13.

(I think this is where you're unclear: when there's no variable shadowing, this.x and x refer to the same instance variable x. The fact that you refer to the variable sometimes as size and sometimes as this.size doesn't mean there's more than one variable [size].)
 
Let's go to the waterfront with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic