In my Java class, I have statements in {}
without any names/references associated with it and it appear to get executed before constructor is run. What is the purpose of it? Is it possible to call it like calling a method by associating a variable/reference to it? If not, can I change the order in which it is triggered?
package com.core.java;
public class App {
public static void main(String[] args) {
new App();
}
static { System.out.print("static block, "); }
App() { System.out.print("constructor, "); }
{ System.out.print("what_is_this? "); }
}
I have seen similar construct in Ruby where it can be associated with a reference and be called at will. For instance
v = -> { puts "A Code Block" }
v.call #=> prints -> A Code Block