Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help with basic concept

 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");}
}
**************************************
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic