<< Wednesday Humor | Home | A Post From Performancing >>

Thursday Quiz

Think you know Java? Try this!

Without the aid of a Java compiler, can you tell if the following class will compile?

public class Foo {
  { Foo(); }
  public Foo() {}
  public void Foo() {}
  public void Foo(Foo Foo) {}
  public static void main(String[] args) {
    Foo Foo = new Foo();
    Foo.Foo(Foo);
  }
}
Tags :


Re: Thursday Quiz

And what's the purpose of this?

Re: Thursday Quiz

I saw some code where a variable is declared with the same name as its class, with confusing code ensueing:

Foo Foo = new Foo();
Foo.someMethod();
...

I thought that would make a pretty interesting quiz. The example is an extremized version.

Re: Thursday Quiz

I'll take a guess and say that it won't compile. At class loading time the Foo(); in the static initializer will require that the Foo() method will need to be static for it to access.

Re: Thursday Quiz

that was originally what I was thinking. However, Foo() is a constructor and that IS legal to call in a static context. Then I wasn't sure what the default return value of a constructor was. I would assume void, so the first two constructors would be conflicting. Apparantly that's not true, though, because running it through a java compiler to test my theory that it would barf on the constructor conflict shows.. well, it shows that it doesn't barf there. :) Don't wanna spoil the answer for anyone else reading.

Re: Thursday Quiz

Actually it isn't a static context. oops. It's merely a context. ;)

Add a comment Send a TrackBack