Pass argument to static constructor in Java?

I am trying to initialize a static class, with an argument, and then run another static code in this class.
I know the static block, but it seems like it cannot take any arguments.
Is there a way to pass arguments to a static constructor?
If not, what is the recommended method to initialize the Static class with an argument?

Edit: For my understanding, a static class is a class that cannot be created (in C # they are called static classes, if Java has a different term for them, sorry for not knowing about it) - it got access to him through the class name, the name of the object.

What I'm trying to achieve (very simplified) is a class that receives a dictionary as a String, parses it, and has methods that manage it like GetRandomEntry .

Here is a snippet of code:

 public class QuestionsRepository { private static Map<String,String[]> easyDefinitions = new HashMap<String,String[]>(); //... static { // need to receive and parse dictionary here } //... 

Accepting the appropriate parts of a code snippet is never easy, hopefully I chose wisely (:
Another detail that may be relevant is, as a rule, I am a C # programmer. Just started learning Java recently.

Thanks.

+7
source share
6 answers

I think you will need to initialize the static fields of the class according to some input. You can do this as follows by calling the static method of another class:

 class ClassToInitialize { static { staticField = ParamPassClass.getParameter(); } private static String staticField; ClassToInitialize() { System.out.println("This is the parameter: " + staticField); } } class ParamPassClass { private static String parameter; static String getParameter() { return parameter; } static void setParameter(String parameter) { ParamPassClass.parameter = parameter; } } class Main { public static void main(String args[]) { ParamPassClass.setParameter("Test param"); new ClassToInitialize(); } } 
+12
source

Java has no static constructors. It has only static initializers, and static initializers do not accept any arguments. It is executed when the class is loaded first, and it cannot be called by itself.

You either need to use real objects, or add a way to configure the class (for example, through a static method).

+2
source

you must specify a member class with a static classifier, otherwise there is no such thing as a static class. Here you can find an explanation of the use of the word "static" in this context.

Now you just have to call its constructor and pass all the arguments you need, the only limitation that you have on the static member class is that it cannot refer to the non-static fields of its outer class, it resembles the static methods of the class, which cannot refer to non-static class fields.

I did not understand why you mention the static initialization block here, could you clarify a little? Keep in mind also that in java there is no such thing as a static constructor ....

Hope this helps

+1
source

You can have a static method public static void setUp(Arg1 arg1, Arg2 arg2...) , which sets all your static fields and calls it when your program starts.

You must make sure that this method will be called only once [or only if you want to reset these fields]

0
source

It is not possible to pass arguments directly to static initializations ( JLS: static initializers ).

It would be nice if you could share more information about your goals.

0
source

You can use an enum to initialize a singlet with a string parameter like this

 import java.util.*; class Data { static Map<String,String[]> easyDefinitions = new HashMap<String,String[]>(); } public enum QuestionsRepository { repository("primary=red,green,blue;secondary=cyan,yellow,magenta"); QuestionsRepository(String dictionary) { String[] rules = dictionary.split(";"); for (String rule:rules) { String[] keyValuePair = rule.split("=",2); Data.easyDefinitions.put(keyValuePair[0],keyValuePair[1].split(",")); } } } 
0
source

All Articles