2

Given the following code:

public class A {
 static final long tooth = 1L;

 static long tooth(long tooth){
  System.out.println(++tooth);
  return ++tooth;
 }

 public static void main(String args[]){
  System.out.println(tooth);
  final long tooth = 2L;
  new A().tooth(tooth);
  System.out.println(tooth);
 }
}

Can you please explain me the concept of shadowing ? And another thing, what tooth is actually used in the code from the main method ?

And i know it's a very ugly code, but ugly is the standard choice for SCJP book writers.

Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118
  • possible duplicate of [What is Shadowing?](http://stackoverflow.com/questions/673779/what-is-shadowing) (although that one is for C#, the concept is the same), and [What is variable shadowing used for in a Java class?](http://stackoverflow.com/questions/1092099/what-is-variable-shadowing-used-for-in-a-java-class) – Péter Török Jul 21 '10 at 15:20
  • @Péter Török - Is there a way to determine if a question is similar to a previous one ? In the related questions section before posting I didn't find (or at least i think so) the links you are referencing. – Andrei Ciobanu Jul 21 '10 at 17:01
  • The SO search engine is known to have deficiencies. Some actually suggest using google to search stackoverflow.com instead. However, tag based searching is useful. In this case both of the questions linked have the `shadowing` tag. – Péter Török Jul 21 '10 at 17:10

2 Answers2

2

There's nothing magical about shadowing as a concept. It's simply that a reference to a name will always be referencing the instance within the nearest enclosing scope. In your example:

public class A {
 static final long tooth#1 = 1L;

 static long tooth#2(long tooth#3){
  System.out.println(++tooth#3);
  return ++tooth#3;
 }

 public static void main(String args[]){
  System.out.println(tooth#1);
  final long tooth#4 = 2L;
  new A().tooth#2(tooth#4);
  System.out.println(tooth#4);
}

}

I've annotated each instance with a number, in the form "tooth#N". Basically any introduction of a name that is already defined somewhere else will eclipse the earlier definition for the rest of that scope.

Gian
  • 13,735
  • 44
  • 51
  • It was a great idea of numbering the teeth in my code, now I get it. It seems that I always forget java is pass by value, and that's I am considering different results of those the compiler is returning. The problem was that I was confusing tooth#3 with tooth#4 and was a little awkward for me that tooth#4 is being modified, as it was declared static. – Andrei Ciobanu Jul 21 '10 at 15:22
1

When you are at this point

System.out.println(tooth);

the class property (static final long tooth = 1L;) is used, then a new tooth is declared, which shadows the class property, meaning that it is used instead of that.

Inside the tooth method the tooth variabile is passed as value, it will not be modified, you can see this by executing the main which gives:

1
3
2
Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106