About static methods

From: Joona I Palaste (palaste_at_cc.helsinki.fi)
Date: 03/30/04


Date: 30 Mar 2004 20:26:29 GMT

Everyone knows Java's ugly and disgusting way of allowing calling
static methods through instance expressions. Some people have said that
the instance expression is only used for its type. That's not strictly
true. Consider this code:

public class Foo
{
  public static Foo method1() {
    System.out.println("Hello there!");
    return null;
  }
 
  public static void method2() {
    System.out.println("Hello again!");
  }
 
  public static void main(String[] args) {
    method1().method2();
  }
}

When run, this code prints:

Hello there!
Hello again!

This means that the instance expression is *evaluated* even if it's
used as a qualifier for a static method. It's *value* is disregarded,
though. This can be seen from the above code, where the value of
"method1()" is null, and no methods can normally be called on null.

-- 
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Last year he disrespected me - and then he showed lack of respect."
   - Anthony Mason