Java class vs. variable access

Here’s an interesting question for all of you Java-ites out there. Given the following code:

class Test {
public Test() {
final Foo Bar = new Foo();
Bar.go();
}
}
class Foo {
public static void go() { /*does something Foo-y*/ }
}
class Bar {
public static void go() { /*does something Bar-y*/ }
}

which go() is called from Test’s constructor?

The answer is that Foo’s go() is called. So now the next question is, how does one call Bar’s go()? The obvious answer is “fully qualify Bar” (i.e. specify it’s package name). But what if Bar is in the default pacakge?

This is obviously a contrived example and most people would say “use cAmEl case for you variable names so that this is never a problem” and you’re right, that’s a perfectly cromulent approach. I actually ran into this in practice when working with JavaScript which is a whole different problem.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s