Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Doubt in final variable

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from K&B's SCJP 5 Chapter 3 Assignments selftest question
public class Knowing {
static final long tooth = 343L;
static long doIt(long tooth) {
System.out.println(++tooth +"");
return ++tooth;
}
public static void main(String[] args) {
System.out.println(tooth + " ");
final long tooth = 340L;
new Knowing().doIt(tooth);
System.out.println(tooth);
}

}
I would have expected a compiler error because there is a variable
static final long tooth in the class
and final long tooth in the main method. Since this is not shadowing and the class variable is static why isn't there a compiler error?
Thanks in advance,
Meera
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can always have a local variable (i.e., a variable defined inside a method) with the same name as a variable of any type in the enclosing class.
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

meera kanekal wrote:Since this is not shadowing and the class variable is static why isn't there a compiler error?



I would like to know why this is not shadowing.

tooth at line 2 shadows tooth at line 1, method local variables can always shadow instance or static variables with the same name.
Same thing applies for tooth at line 3 which shadows tooth at line 1 and hence local variable of line 3 is used in line 4 and line 5.
So, no compile time error.

And please post your code examples inside CODE tags for better readability.
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought shadowing applied only to variables with the same access modifier. For example one is a static variable and the other isn't. Therefore I thought this was not shadowing. However thanks for clearing this up. BTW how do I post the code using tags?
Thanks,
Meera
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I thought shadowing applied only to variables with the same access modifier. For example one is a static variable and the other isn't.


I think that you are trying to mix-up static modifier and access modifiers. Just to remind you that, 'static' is not at all an access modifier.

Anyway, while you create a post/reply, in that page, you will find a lot of buttons above the textarea where you write your message. One of those buttons is named
as 'Code'. Click that button and you will be able to see CODE tags in your message. Put all the code related example inside those CODE tags.
Hope that helps.
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic