Satya,
Your interpratation is right.
Indy,
public static StaticTest2 me(){
// ..
} is a valid method declaration/defn. Generally the format of a method is
accesslevel (public/protected/private/default)
other modifiers static/final/native/abstract/synchronized..etc)
the_return_type ( anyofprimitivesORRefeType(byte/char/short/int/long/float/double ) OR className/interfaceName/array (of primitive/reftype) OR void )
methodName ( parameters.. ) throws Throwable1,Throwable2, .... THrowable(n) So the above method is public and static and returns an instance of Class StaticTest2 and its method name is me().
The interesting part here is
me().run() is a valid code. Since me() returns an instance of class StaticTest2, and we can very well call a method on an object of a class, it is valid. Since all static method calls are bound at compile time itself (since static belongs to the class as a whole and not on any particular instance of the class, the compiler binds them at compile time itself) , here what me().run() does is me() is bound first (since it is static), then , since it returns an instance of StaticTest2, calling run() on that, is also statically bound, since run() is also static. note that static methods can be called as ClassName.staticMethod() as well as anInstanceoftheClass.staticMethod(). Here the compiler treats this case as the anInstanceoftheClass.staticMethod() type.
So both me() and run() are statically bound at compile time itself and so it prints me and run. Another point to note here is a static method can be called on a null object instance, wheras you CAN NOT call an instance method. It will throw a
NullPointerException at run time. For example the foll sample program you can call the static method m1() on the null ref t1 but not m2().
<pre>
class Test {
public static void main(String[] args) {
Test t1=null;
//t1.m1(); //valid
t1.m2();
}
static void m1() {
System.out.println("hello");
}
void m2() {
System.out.println("hello");
}
}
</pre>
regds
maha anna
[This message has been edited by maha anna (edited May 25, 2000).]