File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "To print statement" Watch "To print statement" New topic

To print statement

Rajesh Santhanakrishnan

Joined: Jul 16, 2010
Posts: 29
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.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

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 8 API documentation
    I agree. Here's the link:
    subject: To print statement
    It's not a secret anymore!