aspose file tools*
The moose likes Beginning Java and the fly likes How Static Constructor called first. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How Static Constructor called first." Watch "How Static Constructor called first." New topic
Author

How Static Constructor called first.

midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
hi guys,
Can you explain why and how static constructors are called first? I was trying out some code to know more about static constructors. Here it is


the output is:
this is static constructor
Hi

when i searched more on it, i read this: 'The point of Static methods is that they can be called without creating an instance of a class'.

now i tried one more code(given below) with two classes and i din't get the above point. The static constructor is not invoked without creating instance. Why so?
and what i understood is static constructors are created first but not without I create instance for the class. Why and how static constructors are invoked first. Here is the code i tried:



output:
hi

please explain why the static constructor is not called here when instance sf is not created(note that i have commented that last line intentionally). Please explain how static constructors are invoked first.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hi, and welcome to the Ranch!

midhuna peru wrote:hi guys,
Can you explain why and how static constructors are called first?


They're called static initailizers, not static constructors. And they're called first because the spec says the have to be, and the JVM is written to conform to the spec.

when i searched more on it, i read this: 'The point of Static methods is that they can be called without creating an instance of a class'.


Which has nothing to do with static initializers. Static initializers are called exactly once, when the class is loaded. Static methods are called whenever you call them.

The static constructor is not invoked without creating instance.


No, this is false. The static initializer is invoked before any method can be called or before any instance can be created.

please explain why the static constructor is not called here when instance sf is not created


Static initializers are called when the class is loaded. If you never use the class, it's never loaded, so the static initializers are not invoked. Note that loading a class is completely different from creating an instance.
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Hi,

Actually, the static block/member is executed at the time of class loading. So whenever a class is loaded, the static portion of it is executed. So, in your case the classloader for StaticTry is not executed.

Vishal


Programming is about thinking, NOT coding
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Jeff Verdegan wrote:Hi, and welcome to the Ranch!
Agree
… the spec says …
And you can read the specification here. You probably don’t need to read the whole chapter, and it can be hard to read.
midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
Thanks guys! doubts cleared. Got it
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Static Constructor called first.