• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding static blocks  RSS feed

 
raj talatam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


how to execute This code block
{
System.out.println("in a code block..........");
}
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The non-static initialization code block:


is not a static code block, it is executed when an instance of the class NoMain is created. So to execute it create an instance of NoMain.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, why there is System.exit(0) in static block?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:By the way, why there is System.exit(0) in static block?


This code is an answer to the age-old question 'How do you start a java application without a main() method? The answer is you can use a static block to start the application instead. But at the end of the block you need to use System.exit(0) to prevent the JVM throwing an error saying the main method couldn't be found.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:
Anayonkar Shivalkar wrote:By the way, why there is System.exit(0) in static block?


This code is an answer to the age-old question 'How do you start a java application without a main() method? The answer is you can use a static block to start the application instead. But at the end of the block you need to use System.exit(0) to prevent the JVM throwing an error saying the main method couldn't be found.


I didn't notice the absence of PSVM method
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!