Questions tagged [static-block]

A static block is a normal block of code enclosed in braces, { }, and preceded by the static keyword.

166 questions
10
votes
3 answers

Declare static property in Kotlin?

My Java code: public class Common { public static ModelPengguna currentModelPengguna; }
Kang sigit
  • 109
  • 1
  • 3
10
votes
5 answers

What is static block in c or c++?

I want to know that what is static block in c or c++ with an example? I know what is static but what is the difference between static and static block?
Abhineet
  • 6,459
  • 10
  • 35
  • 53
9
votes
2 answers

Can static code blocks throw exceptions?

In a hypothetical situation I have a class like this: import java.io.File; import java.util.Scanner; class X { static Scanner scanner; static { scanner = new Scanner(new File("X.txt")); } } When compiling, I get unreported…
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
8
votes
1 answer

abstract class inside static block usage

I can add abstract keyword inside static initialization block, but I can't add abstract method as abstract void draw(); So I can only add abstract class inside static block, as follows: static { abstract class Abstract { abstract…
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
8
votes
2 answers

Why static block is not executed

As per java doc, static block is executed when the class is initialized. Could anyone please tell me why static block is not executed when I run below code? class A { static { System.out.println("Static Block"); } } public class…
7
votes
3 answers

When the static block will be executed in JAVA while creating object?

class DemoClass { public static void main(String args[]) { System.out.println("Start"); A a=new D(); } } class A { static { System.out.println("Static A"); A c=new C(); } public A() { …
Wall-E
  • 119
  • 5
7
votes
2 answers

Static block not executed without initialization

I have a question regarding static blocks: Let's say i've got a class looking like this: class SomeClass { static { System.out.println("static block"); } } and I define a variable of type SomeClass somewhere. public static void…
ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
7
votes
6 answers

Singleton object- In static block or in getInstance(); which should be used

Below are two ways to implement a singleton. What are the advantages and disadvantages of each? Static initialization: class Singleton { private Singleton instance; static { instance = new Singleton(); } public Singleton…
Rahul Rastogi
  • 4,486
  • 5
  • 32
  • 51
6
votes
1 answer

Why Object class have static block?

I want to just know about why Object,String etc. have static{} block at the end.what is the use of static block in Object Class. Open the cmd prompt and type javap java.lang.Object
6
votes
3 answers

When objects created with static reference, why do instance block & default constructor get executed before static block?

public class TestLab { static Test aStatic=new Test(); public static void main(String[] args) { TestLab obj=new TestLab(); } static{ System.out.println("In static block of TestLab"); } } public class…
Arijit Dasgupta
  • 325
  • 3
  • 14
6
votes
1 answer

Magento How to link to category by id from static block/page

I'm looking to link to a category from a static block using the category id. Any thoughts? I've done the usual searches, but to no avail. At the moment I can do something like , but this is hardly…
Alex Hadley
  • 2,125
  • 2
  • 28
  • 50
5
votes
1 answer

When linking, is there something between "grab what you need" and "grab all" (-Wl,--whole-archive)?

I have this library which involves some static initialization code which needs to run before main(). It all works well if you just compile all of the translation units together, but it doesn't work if I provide a static library (.a file) and have…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
5
votes
3 answers

How the order of execution is performed between static variables and blocks?

Consider the below scenario: Code:1 public class StaticDemo { static{ b=5; System.out.println("Static B:"+b);/*Compilation error:"Cannot reference a field before it is defined"*/ } static int b; static{ …
Vasanth
  • 129
  • 9
5
votes
2 answers

Where in memory are objects located when they are created within a static block?

If I create a static block and create an Object there, say of some other class, will the object be created on the heap or on the stack? class Hello { static { Abc abcObject=new Abc(); } // Other Code... }
Kumar Ritesh
  • 183
  • 1
  • 2
  • 11
4
votes
1 answer

Dalvik classloader mystery

I am on Android 2.2 SDK and could not get my static block inside MultiUserChat class to execute. I have tried to force load it as try { String qual = MultiUserChat.class.getName(); ClassLoader.getSystemClassLoader().loadClass(qual); …
kellogs
  • 2,837
  • 3
  • 38
  • 51
1
2
3
11 12