File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes To print statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "To print statement" Watch "To print statement" New topic
Author

To print statement

Rajesh Santhanakrishnan
Greenhorn

Joined: Jul 16, 2010
Posts: 15
Hai ,
Consider the below program.

How to execute the print statement above without adding method or crreating the instance of the class.I was told that it is possible.Hence i am posting the question.

Thanks
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Welcome to JavaRanch. Please UseCodeTags when you post source code, so that it's easier to read your code. I've added them for you in your post above (I can edit your post because I'm a moderator on this forum).

The println statement in line 9 is in an instance initializer block. It will be executed when you create a new instance of the class Test. If you change it to a static initializer block, it will be called when the class is loaded - so then you don't need to create an instance of the class:

More information here:
  • The Essence of OOP using Java, Instance Initializers
  • The Essence of OOP using Java, Static Initializer Blocks


  • Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
    Scala Notes - My blog about Scala
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: To print statement
     
    Similar Threads
    read-write reordering question
    static and non-static can be in the same class...they will fight:-))
    methods will execute?
    Static method overloading
    SCJP Brainteaser (11)