0

Is there any way to access a static member of a nested class from the enclosing class when the nested class shares a name with a static member of the enclosing class? For example:

package a; class a { static Object b; static class b { static Object foo; } static Object needAccessToFoo; static { // can I access foo? } } 

The class b (as opposed to the member b) can be accessed when being used as a Type via a (or [b.]b.a). And foo can be accessed using the instance of the nested class b as so:

static { [a.][a.]b bar = new b(); needAccessToFoo = bar.foo; } 

However, since b is a nested class and not an inner class, it leaves one to wonder if there is a proper way to statically reference foo without going through an instance of b (the object referred to by bar). Not to mention it's generally accepted as bad practice to access a static member via an object.

4
  • I get "java: class a is already defined in package a package" Why would you use a lower case name for a class anyway? Commented Nov 4, 2013 at 22:47
  • @PeterLawrey I changed the example so it compiles. Commented Nov 4, 2013 at 22:55
  • Can you change it so it conforms to Java Coding Conventions with B for the class and b for the field? ;) Commented Nov 4, 2013 at 22:57
  • 1
    @PeterLawrey well that would defeat the entire point of the question, wouldn't it (; Commented Nov 4, 2013 at 22:58

2 Answers 2

1

This problem is known as Obscuring. The JLS states

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.

It won't be possible to access foo.

In the context of the static initializer block there are two things with the name b. And because of the rule above, the variable will always be chosen when using that conflicting name. Qualifying the variable with its enclosing type, a.b doesn't change anything because the variable is static and therefore accessible through that reference.

Sign up to request clarification or add additional context in comments.

2 Comments

The bolded text does say simple name. However, are you suggesting I take that to mean no fully-qualified name either? In that case, the implication would be there there are some cases in which you cannot access a reference, which seems unlikely (hence my question).
@David Yes, there are some cases were you would not be able to access the reference. This is one of those cases. Note, that you can access foo inside b.
0

Yes since everything is static and a is in scope of your static block you can do:

static { Object foo2 = a.foo; // can I access foo? }

You will need to rename your outer class though to prevent a name collision. Otherwise how will the compiler know which a you are referring to?

1 Comment

Yeah sorry I just finished editing it so it compiles.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.