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

Package and nested classes of same name

 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something just worked that I was a bit surprised by. I wondered if someone could point me to the relevant JLS area, so I can understand what is going on. I had a quick look myself, but the relevant bit didn't jump out at me.

The situation is something like this...

Some old code has a top-level class FooImpl in a particular package, implementing interface Foo. A new top-level class Bar that I'm writing in the same package has a different implementation of Foo, which is a nested (some might say "static inner") class also called FooImpl.

Class Bar happily chooses to use the new FooImpl nested class, not the old top-level FooImpl. I thought it might get confused by the presence of a class called FooImpl in the same package.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this in the JLS: 6.3.1 Shadowing Declarations
[ February 12, 2007: Message edited by: Jesper Young ]
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Er, no. I'm not importing anything. That's the point. All is in the same package.

Let me try to illustrate with code: -

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Jesper wrote:

Outside the Bar class, FooImpl refers to the top-level class, the nested class has the name Bar.FooImpl.

Inside the Bar class, the nested class shadows the top-level class - you'd have to use the fully qualified class name to access the latter.

At least that's my current understanding without any deeper analysis on my part...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic