• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

name conflict

 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why no error for line 1 but line 2 does not compile....
 
Dan Lastoria
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the statement System.out.println(Test.Test) The compiler will access the type "Test" based on the nearest scope. In your case "Test" references the primitive int static field. So what you're trying to do is like saying "for the primitive static int field named Test give me member Test", which doesn't exist.
The compiler is not seeing Test.Test as "Class Test.Static Member Field Test". It is seeing it as "Static Member Field Test.No Such Member".
[ August 18, 2003: Message edited by: Dan Lastoria ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I did not think it that way.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to have a go at explaining this, because I am not good at determining the meaning of a name. I need the practice.
System.out.println(Test.Test);
1. The name �Test.Test� does not exist in a context (JLS 6.5.1) where it is clearly a package name, type name, expression name, method name or package or type name. So it is classified as an ambiguous name.
2. Then the ambiguous name �Test.Test� is reclassified (JLS 6.5.2). Since it is a qualified name, the name to the left of the dot �Test� is reclassified. This name does not exist in a context (JLS 6.5.1) where it is clearly a package name, type name, expression name, method name or package or type name. So it is classified as an ambiguous name.
3. Then the ambiguous name �Test� is reclassified (JLS 6.5.2). Since it is a simple name, and it appears within the scope of a local variable declaration (no) or parameter declaration (no) or field declaration (yes) with that name, it is reclassified as an expression name.
4. Now that �Test� to the left of the dot has been classified as an expression name (i.e. an identifier of a field declaration), the name to the right of the dot must be a member of the type of that field. But the type of that field is a primitive int.
So we get the compiler error : int cannot be dereferenced
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic