This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Static block  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am writing junit test for a class which has a static block on it.

I have many test methods which is accessing the class using .class . This doesnt call the static block

Example


What can be done if I want this static block to be executed ?
 
Marshal
Posts: 63849
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By static block, you probably mean a static initialiser.You can't call it. A static initialiser is called once, when the class is loaded, and never again. It is no more possible explicitly to call it than it is to call a constructor on a extant object.
 
Usha Viswanathan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:By static block, you probably mean a static initialiser.You can't call it. A static initialiser is called once, when the class is loaded, and never again. It is no more possible explicitly to call it than it is to call a constructor on a extant object.




Hi,

Even once also not getting called in my snippet of code. I am asking why is it so ?
 
Rancher
Posts: 3989
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, we can't see your code so can't say, but we can say that, on loading the class file, Java will execute a static block.

How do you know it isn't executing?
 
Usha Viswanathan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Below is the sample code I am running





On executing the above class with main, my statement on previous class "Hello" is not printing
 
Rancher
Posts: 1943
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to instantiate/load the class to invoke the static initialiser block.  It will be called once when the class is loaded.

 
Campbell Ritchie
Marshal
Posts: 63849
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Usha Viswanathan wrote:. . . . . .

What makes you think that code causes the StaticClassTest class to be loaded? It loads the Class<StaticClassTest> class and creates an instance of it. Try Class.forName("StaticClassTest");
 
Dave Tolls
Rancher
Posts: 3989
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I thought loading would cause the static to run.

So I've had a play around.

Using the follwing basic main method:

the following Bar static does not run:

Now, I know that loading does not necessarily mean instantiating the class if it's not actually used.
So I changed Bar to include a static X, and main to print Bar.X.


Now, X prints 1...but still no sys out.

If, however, I move the initialise of X into the static:

I finally get an output.

So, I'm guessing it only loads what it needs to?
 
Campbell Ritchie
Marshal
Posts: 63849
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Actually, I thought loading would cause the static to run. . . . So, I'm guessing it only loads what it needs to?

Obviously the static initialiser is intended for initialsing fields, and if there aren't any fields, it only executes what it has to.
 
Usha Viswanathan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Usha Viswanathan wrote:. . . . . .

What makes you think that code causes the StaticClassTest class to be loaded? It loads the Class<StaticClassTest> class and creates an instance of it. Try Class.forName("StaticClassTest");



Whatever we call after the "Class." will be called right ? That is what I thought of.

I thought calling "StaticClassTest.class" itself will make my static block inside StaticClassTest to be called

Since it is static, you dont need to create an instance of a class

So wanted a reason why my sysout inside the static block is not running
 
Campbell Ritchie
Marshal
Posts: 63849
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Usha Viswanathan wrote:. . . I thought calling "StaticClassTest.class" itself will make my static block inside StaticClassTest to be called . . .

No, it creates a Class<StaticClassTest> object to be created. If you look at Dave Tolls' post you will find the static initialser was only called when it was necessary to initialise the field. Writing Foo.class does not mean you are using the Foo class in he first place, so it does not necessarily cause the Foo class to be loaded.
 
Dave Tolls
Rancher
Posts: 3989
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The initialisation section of the JLS covers this (section 12.4).
A class is initialisaed when:

"
T is a class and an instance of T is created.

A static method declared by T is invoked.

A static field declared by T is assigned.

A static field declared by T is used and the field is not a constant variable (ยง4.12.4).
"

So, from my example above, in the first case (using Bar.class) none of the above come close to applying.
In the second case, with X declared and assigned in one go, it's almost part 4, but as it's a constant (final) it actually doesn't cause initialisation.
Only in the third case, where X is assigned in the static block does it force initialisation.
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!