File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Question on - System.out.println method (static Vs non-static) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question on - System.out.println method (static Vs non-static)" Watch "Question on - System.out.println method (static Vs non-static)" New topic
Author

Question on - System.out.println method (static Vs non-static)

Malli Raman
Ranch Hand

Joined: Nov 07, 2001
Posts: 312
Hi,

How do we can call a non-static method with in a static method.
i.e my question how we can call System.out.println inside the static block.
Thanks & Regards,
M.S.Raman
public class HelloWorld
{
static
{
java.io.PrintStream out = System.out;
out.println("Hello World"); // Non Static method..
System.out.println("Hello World");
print();
}

public static void print() // If we remove static the class won't compile
{
System.out.println(String.valueOf("Hello World"));
}

public static void main(String args[])
{
}
}
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
simple answer: you don't.
The static block is not executed for any specific instance of the class and therefore cannot depend on any instance of the class.
Ergo you cannot use a non-static method of the class in the static block of the class because any non-static method exists only in the context of an instance of the class.


42
Azriel Abramovich
Ranch Hand

Joined: Dec 10, 2003
Posts: 38
A non-static method can be called within the scope of an instance, therefore you have to create a local instance of your class for example


Don't be shy, be quiet!
Malli Raman
Ranch Hand

Joined: Nov 07, 2001
Posts: 312
Originally posted by Jeroen Wenting:
simple answer: you don't.
The static block is not executed for any specific instance of the class and therefore cannot depend on any instance of the class.
Ergo you cannot use a non-static method of the class in the static block of the class because any non-static method exists only in the context of an instance of the class.

Hi,
I agree what you said. I think i misunderstood about the static block, methods and variables. I thought all those static variables, methods are get initialised before the object creation. It seems to be wrong. What is the use of static? (without creating the object we can call the method using the class name). How it is stored in the jvm.
Regards,
M.S.Raman
Malli Raman
Ranch Hand

Joined: Nov 07, 2001
Posts: 312
Hi,
I cleared my doubt.. Once again thanks for replying my post.
1. Static block is called first while loading the class in the jvm.
2. You can call static method inside the non-static method using the class name and to call a non- static method inside a static block , we have to call the method using their objects.
Regards,
M.S.Raman
Originally posted by Malli Subramanian Raman:

Hi,
I agree what you said. I think i misunderstood about the static block, methods and variables. I thought all those static variables, methods are get initialised before the object creation. It seems to be wrong. What is the use of static? (without creating the object we can call the method using the class name). How it is stored in the jvm.
Regards,
M.S.Raman
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on - System.out.println method (static Vs non-static)