• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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: 24207
46
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.
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic