Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass argument to a static constructor in Java?

I'm trying to initialize a static class, with an argument, and then run some more static code in that class.
I'm aware of the static block, but it seems it can't take any arguments.
Is there a way to pass arguments to a static constructor?
If not, what is the recommended technique to initialize a Static class using an argument?

Edit: A static class to my understanding is a class which cannot be instantiated (in c# they're called static classes, if Java has a different term for them, sorry for not being aware of it) - it's accessed through it's class name rather than an object name.

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

Here's an elaborated snippet of my code:

public class QuestionsRepository {  
private static Map<String,String[]> easyDefinitions = new HashMap<String,String[]>();  

//...  

static 
    {  
    // need to receive and parse dictionary here    
    }  
//...   

Taking the relevant parts of a code snippet is never easy, hope i have chosen wisely (:
Another detail that may be relevant - I'm a c# programmer, usually. Just Started learning Java lately.

Thanks.

like image 339
Oren A Avatar asked Jan 29 '12 10:01

Oren A


People also ask

Can we pass parameter to static constructor?

A static constructor doesn't take access modifiers or have parameters. A class or struct can only have one static constructor. Static constructors cannot be inherited or overloaded. A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR).

Can we pass argument in constructor in Java?

You can pass an argument of any data type into a method or a constructor. This includes primitive data types, such as doubles, floats, and integers, as you saw in the computePayment method, and reference data types, such as classes and arrays. Here's an example of a factory method that accepts an array as an argument.

What will happen if we pass parameters to static constructor?

A static constructor is called automatically to initialize the class before the first instance is created, so we can't send it any parameters. You cant pass parameters to Static Constructors, because you cannot access any non-static member outside of a static method (constructor too).


1 Answers

I think you would need to initialize the static fields of the class according to some input. You can do it in the following way 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();
    }
}
like image 135
James Jithin Avatar answered Nov 15 '22 21:11

James Jithin