This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes static doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static doubt" Watch "static doubt" New topic

static doubt

sharma anurag

Joined: Aug 29, 2007
Posts: 16
class Question10



public static void main(String[] args)
new Question10();
Ans: is 3,2,1.
Hi all,
as of my knowledge static block will execute first in the above program but why is it printing the statement about the static block before the constructor block?
According to me it should print 3,1,2. Please help me
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24190


Welcome to JavaRanch!

There are three print statements in the above. In order, they appear in (1) the constructor, (2) an initializer block, and (3) a static initializer block. Static initializer blocks are executed right after a class is loaded, so that's why "3" is printed first. The other initializer blocks are inserted into each constructor, right after the "super" call and before the statements in the body of the constructor; that's why "2" is printed next, as it's the first thing that happens in the constructor.

[Jess in Action][AskingGoodQuestions]
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: static doubt
It's not a secret anymore!