This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What is the use of static block 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is the use of static block" Watch "What is the use of static block" New topic
Author

What is the use of static block

V Aravind
Greenhorn

Joined: Jul 20, 2010
Posts: 3
What is the use of static block in java.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
One use is to hold code that you want to run immediately when the class is loaded,
before any objects are instantiated. For an inheritance tree, this means that the
order of execution of the static blocks is from subclass on up. (Execution of any
non-static blocks is from superclass on down.) How is this useful? I'd just say,
very special situations.

Jim ... ...


BEE MBA PMP SCJP-6
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

V Aravind wrote:What is the use of static block in java.


Mostly used for initialized static variables.
Zhiwei Huang
Greenhorn

Joined: Jul 06, 2010
Posts: 4
Jim Hoglund wrote:One use is to hold code that you want to run immediately when the class is loaded,
before any objects are instantiated. For an inheritance tree, this means that the
order of execution of the static blocks is from subclass on up. (Execution of any
non-static blocks is from superclass on down.) How is this useful? I'd just say,
very special situations.

Jim ... ...


So if I have

the output would be BA?
Vicky Mehta
Greenhorn

Joined: Jul 15, 2010
Posts: 15
the output would be BA?

ouptut is AB


Preparing for SCJP
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Well, I thought so, but my bad. Hi Vicky and welcome to JavaRanch.
Below, with output, is a bit different than the example above. Jim ... ...
Vicky Mehta
Greenhorn

Joined: Jul 15, 2010
Posts: 15
Thanks Jim.
I found another good example in JavaRanch Forums.... Glad to be part of this forum
http://www.coderanch.com/t/497400/java-programmer-SCJP/certification/static-instance-initializer-blocks-inheritance
akhter wahab
Ranch Hand

Joined: Mar 02, 2009
Posts: 151

Vicky Mehta wrote:Thanks Jim.
I found another good example in JavaRanch Forums.... Glad to be part of this forum
http://www.coderanch.com/t/497400/java-programmer-SCJP/certification/static-instance-initializer-blocks-inheritance


Good One


Start Earning Online||Start Earning Using Java
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the use of static block
 
Similar Threads
I hate NullPointerExceptions!
static initializer block
static doubt
When to use Static initial block?
static block