A static block is a normal block of code enclosed in braces, { }, and preceded by the static keyword.
Questions tagged [static-block]
166 questions
0
votes
1 answer
Is there a way to Implement conditional statements in Static Block
I have a class HDFSHbaseInstance which will load Hbase config in static block
public class HDFSHbaseInstance {
private static FileSystem hadoopFS = null;
private static Configuration config = null;
static {
try {
…

Shashank Gb
- 902
- 1
- 6
- 14
0
votes
1 answer
Move the contents of this initializer to a standard constructor or to field initializers
As I installed Sonarlint plugin in my eclipse IDE, i got the above (Heading) Major Issue in Sonar Report.
The Code is:
public class Demo
{
public static final Map CARD_TYPES;
static
{
CARD_TYPES = new HashMap

Sunil
- 71
- 1
- 11
0
votes
1 answer
Static block structure changing after compilation
I'm declaring a static variable after static block. When I'm calling a method to print its value, the result is 0. I decompiled the .class file and found that the structure of the static block has changed. Can anyone please explain why?
class…

Abhishek Sahu
- 13
- 3
0
votes
1 answer
Declaring static initializer block before declaring variable explanation
How does this code work fine and prints 9?
public class Dog{
static {
age=9;
}
static int age=7;
}
And this code doesn't compile(illegal forward reference)? Notice I changed age in static block.
public class Dog{
static…

Stefan Jankovic
- 53
- 4
0
votes
1 answer
Can I use a field from a static block and assign it to the class field?
I want to number the races depending on their location and it should be done when creating the car in the constructor. The question is can I get a field from a static block and assign its value to the vehicle class field?
public class Vehicle {
…

radek.jankowski
- 105
- 8
0
votes
4 answers
Eager Initialization over static block
As I'm trying to understand things better, I'm realizing how less I know. I'm sorry, if this sounds like a simple or silly question.
Do we really need static block, if it is ONLY for initialization of STATIC variables without any other logic coded…

ConfusedCoder
- 91
- 6
0
votes
1 answer
Magento 1.9 hide CMS Static on Cart and OonePage .phtml
Been looking at this online but didn't seem to find an answer that relates to my problem.
I have a static block on top of my page. Very simple one
Ja7gabd
- 1

Ja7gabd
- 1
0
votes
1 answer
Alternative to static blocks without unused variable
As an alternative to static blocks, Oracle docs are suggesting to call a method(s) and the example is using a variable assignment:
public static varType myVar = initializeClassVariable();
The advantage of private static methods is that they can be…

Ori Marko
- 56,308
- 23
- 131
- 233
0
votes
1 answer
static block not called when class is loaded
I was under the impression that a class's static initialization block gets called when the class is loaded.
(For example see this answer:
https://stackoverflow.com/a/9130560/889742
)
But this test shows that the static block is not called at class…

Gonen I
- 5,576
- 1
- 29
- 60
0
votes
1 answer
Submit Callable with ExecutorService in static block
I encountered a strange problem while using ExecutorService to execute a Callable in static block. let's see the complete code directly as below.
package com.acme;
import java.util.concurrent.Callable;
import…

Sunny
- 129
- 1
- 3
0
votes
0 answers
Static Block in jquery .html()
Is it possible to add a static block within .html()? I'm trying to switch between static blocks if window width is greater than 1024px.
$(window).ready(function() {
var ver = $(window).width();
if (ver > 1024)…

Billy
- 126
- 1
- 10
0
votes
1 answer
Mocking getResource in static block with PowerMock
How to mock getResourceAsStream in the static block?
I think it is untestable.
I reviewed SO and cannot find the answer. The closes-SO-post-here does not address the issue as the call to getResourceAsAStream in the post is not from a static block.
I…

Witold Kaczurba
- 9,845
- 3
- 58
- 67
0
votes
1 answer
Magento: Insert Static Block into the Progress block of the Onepage Checkout
I am trying to add my static block into the Magento one page checkout. More specifically under the Progress section of the page (see attached image). Unfortunately I cannot find anything online nor does my code seem to work. Any ideas?
local.xml
…

Daniel Vickers
- 1,054
- 1
- 12
- 32
0
votes
1 answer
how to change new products block heading in magento 1.9
I have added a new products static block with widget on my home page. I am not able to change heading of this block. Is there any way to change it.

Amit Kumar
- 2,141
- 1
- 11
- 13
0
votes
1 answer
Java Pattern for Classes Inheriting Static Block
Thanks in advance for the help!
I have a few classes that need to all include the same static block, like:
public class Class1 {
static {
changeState();
}
...
}
public class Class2 {
static {
changeState();
}
…

markw
- 321
- 1
- 3
- 14