File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Malli Raman
Ranch Hand
Posts: 312
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Azriel Abramovich
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Malli Raman
Ranch Hand
Posts: 312
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 312
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic