Search code examples
javajavac

Why isn't the java compiler complaining at this:


I have a line of code that is essentially this

NameOfAClassOnTheClasspath.

The compiler accepts this without an error or even a warning. I've tried this in eclipse and on the command line.

What on earth can the compiler (javac 1.6) even think this means and why isn't it complaining?


Solution

  • Since whitespace is ignored, something like

    SomeClass.
    
    staticMethod();
    

    Is a perfectly legal way to call

    SomeClass.staticMethod();
    

    (You can replace the method with pretty much any other static member of the class)

    e.g. an inner interface of another interface:

    Map.
    
    Entry<Integer,Integer> x; // Declares a Map.Entry<Integer,Integer>
    

    Of course it's pretty terrible coding style, unless you're breaking lines that are too long to fit on the screen, and in that case you should indent the rest of the line.