static methods do not have this or super ie compiler only provides for objects in place of this and super for non-static methods only. right ? then how come this compiles and runs fine. the main method is able to call the static method test() fine. hence it must have done it via this.test(). right ? because main is also a static method. i think i am not ok with this..... ************************************* class A { public static void main(String args[]) { System.out.println("del-class1"); test(); } static void test() {System.out.println("sand-method");} } **************************************
Consider that main() and test() are both static methods in class A. Since they both get initialized at class-loading time, they're visible to each other. main() just happens to be the static method that the JVM looks for a class is invoked by name. So when main() calls test(), it only has to lookup a method in its own namespace. We don't typically spend much time thinking about this behavior in Java because it doesn't dynamic object behavior, but it is in fact expected behavior. You can't call a static method from an object without a reference, but one static method can call another that way if they're in the same class.
Make visible what, without you, might perhaps never have been seen. - Robert Bresson
mark stone
Ranch Hand
Joined: Dec 18, 2001
Posts: 417
posted
0
thanks mike. so just to confirm again, that there is no prefix "this" involved, when one static method calls another. (because we know that this and super are only for non-static methods). And as you said they can call each other as long as they are in the same class. right ? i had asked earlier also in one post but got no response, what is namespace ?
Originally posted by Michael Ernest: Consider that main() and test() are both static methods in class A. Since they both get initialized at class-loading time, they're visible to each other. main() just happens to be the static method that the JVM looks for a class is invoked by name. So when main() calls test(), it only has to lookup a method in its own namespace. We don't typically spend much time thinking about this behavior in Java because it doesn't dynamic object behavior, but it is in fact expected behavior. You can't call a static method from an object without a reference, but one static method can call another that way if they're in the same class.
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
posted
0
I think I remember a post of your own where you asked about the term namespace regarding the names made available by an import statement. Yes I remember, you weren't the person introducing the term. I think is not a proper term to be used in that context. In fact JLS doesn't use it. I think what the writer was meaning, is the set of names made directly (without qualifier) available by an import statement. You could call that a namespace but it could be confusing because the term has a meaning in ClassLoaders area.