• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How Static Constructor called first.

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys! doubts cleared. Got it
 
Campbell Ritchie
Marshal
Posts: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
I'm full of tinier men! And a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic