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
2 answers
When is the static block instantiated in a Java Class
Suppose there is a static block in a Class
public class Menu {
private static Map buttonEventMap = new HashMap();
static {
buttonEventMap.put("show-user","show");
…

Isuru
- 7,893
- 8
- 30
- 38
0
votes
1 answer
Magento Static Block is at a Different Place on Internet Explorer than on Chrome, Firefox
I created 3 static blocks ('vert_nav_link', 'social_network', and 'vert_goto_hapi') on a magento site and placed them on the left column (sidebar). On Chrome, Firefox and Safari, they are all shown on the left sidebar. On IE 8, the last static…

user965725
- 1
- 3
-1
votes
1 answer
Using initializing blocks into conditional statements
I've tested the scenario with initializing blocks,
the problem is that if we declare initializing block into if conditional or other conditional statements, initializing block runs first when object initializes.
What the purpose of using…

Vahobjon Tolibov
- 41
- 4
-1
votes
3 answers
Why does a variable declared in static block when used in main method is not not found?
I have a little difficulty understanding how the static block works
import java.io.*;
import java.util.*;
public class Solution {
static {
Scanner sc = new Scanner(System.in);
int B = sc.nextInt();
int H…
-1
votes
2 answers
How to access static block variables outside of class in java
I was working on some code in which I need to access the variable "hs" present in the static block of one class from another.
Note: Both the class are preset in different packages.
Code is as follow:
public class A{
static {
…

user11498795
- 41
- 4
-1
votes
2 answers
Java - Why the instance block is called after the constructor or order wise in the constructor?
As we know that the instance block is called before the constructor. Specifications, Stack Overflow Answer etc.
So, the output that we expect from the code below:
class Test{
static { System.out.println("Static Block"); }
{…

Stack Overflow
- 1
- 5
- 23
- 51
-1
votes
1 answer
Fetching data from a CSV file and storing it into a 2D array - Java
I have a csv file saved with the name - "public/csvFiles/pushup.csv", where I have converted a 2D matrix into csv format.
Now I want to make a static method in a manager class, where in I will make a 2D array by fetching data from that csv file.
So…

Rehmanali Momin
- 199
- 1
- 1
- 14
-1
votes
1 answer
static block with inheritance
I am revisiting the concepts of java. So, I am looking in this example
class A {
A( ) {System.out.print("CA");}
static {System.out.print("SA");}
}
class B extends A {
B() {System.out.print("CB");}
static {System.out.print("SB");}
…

Ankit
- 2,126
- 4
- 33
- 53
-1
votes
5 answers
Object initialization failed in static block
I want to create an array of class objects and initialize it also without using any method so i wrote code like this:
package test;
public class Test2 {
private Test2() {
}
static Test2[] arr = new Test2[10];
static {
for…

Shivam Bansal
- 37
- 1
- 6
-2
votes
2 answers
how to execute static block after main method in java?
I want to "execute" main() method before the static block is called. As per Java rules Static block will be executed when the class is loading and then main() method is called. Is there any way to first "execute" main method and then static…

Trupti Prajapati
- 147
- 1
- 7
-2
votes
1 answer
ClassFormatError array size > 65535
I have one generated class which get this error. Inside this class, there is one huge static block (5000+ lines). I broke the block into several smaller static blocks but still got this error. Why is that
Edit
Code looks like:
private static final…

Edmond
- 614
- 2
- 11
- 26
-3
votes
1 answer
Why compiler shows error when I declare interface in static block in java?
for example
public class Test
{
static
{
interface ITest
{}
}
}
Here the interface ITest is declare within the static block...
The purpose is to know why this happening
Interfaces are inherently static then why it can not be declared…

027
- 1,553
- 13
- 23
-3
votes
1 answer
Static block vs Static Variable initialization order
Scenario 1 :
class A{
static int foo=56789;
static{
foo=999;
}
public static void main(String[] args) {
System.out.println(foo);
}
}
Output : 999
Scenario 2:
class A{
static {
foo=999;
}
static int foo=56789;
public static…

atti
- 180
- 2
- 7
-4
votes
1 answer
Advantages and Usage of Static blocks in Java
please state the advantages of use of static blocks in java. Why we need static blocks in our program.

Dis_Pro
- 633
- 1
- 10
- 21
-5
votes
4 answers
How to execute a static block for multiple times in java
I have given a task to execute static block for every 20 seconds. I have a class that consists of a static block.
public class Hello{
static{
System.out.println("helloo...");
}
}
I know that static block executes when the class…

Prudhvi Bellam
- 71
- 2
- 12