• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To print statement

 
Rajesh Santhanakrishnan
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic