Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static inner class and static member of a class shares SAME NAME? [duplicate]

How can the static inner class M and static member M [of class C] share the same name?

The following code generates "White" as output:

public class Amazed{

    public static void main(String[] args) {
        System.out.println(B.M.W);
    }
}

class B {
    public static class M {       
        static String W = "Black";
    }

    static C M = new C();
}

class C {
    String W = "White";
}

how the member object is accessed and not the static class member : W ["Black"]

if i want to access the member within static class M how to do that?

like image 738
Dineshkumar Avatar asked Jul 26 '13 17:07

Dineshkumar


2 Answers

Chapter 6 of the Java Language Specification (especially Section 6.5) spells out the gruesome details of how Java sorts out what a particular identifier means in a particular context. The rules are fairly complex, but, roughly speaking, Java has six name spaces:

  • Package names
  • type names
  • field (variable) names
  • method names
  • local variable names (including parameters)
  • labels

The same identifier can be used for entities in each of these name spaces. Note that type (class) names and field names live separately, which explains why your code is legal.

Inherited names in the same name space can also sometimes be shadowed or masked. Sometimes an identifier is ambiguous; it then needs to be qualified in some way (e.g., with a package name) or the compiler will complain.

Code obfuscators use this to great advantage, to the point where you can end up with a package named a.a and a class named a in package a (which also would be identified as a.a). Not to mention that Java key words like do and for are legal names in .class files (but not in Java source). It helps make reverse engineering a real bear.

like image 174
Ted Hopp Avatar answered Oct 29 '22 08:10

Ted Hopp


The variable obscures the type of the same name. If a name could be interpreted to be either a variable or a type, the variable is preferred.

You can avoid this by not giving them the same name.

From the Java Language Specification, section 6.4.2: Obscuring:

6.4.2. Obscuring

A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type, or a package. In these situations, the rules of §6.5 specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package. Thus, it is may sometimes be impossible to refer to a visible type or package declaration via its simple name. We say that such a declaration is obscured.

like image 37
Andy Thomas Avatar answered Oct 29 '22 07:10

Andy Thomas