File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help with basic concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "help with basic concept" Watch "help with basic concept" New topic
Author

help with basic concept

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
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

Joined: Oct 25, 2000
Posts: 7292

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


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
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
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.

SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with basic concept