Is Method local variable will garbage collected if stored in static storage?

Viewed 250

I am creating a client class, which will connect to different type of database. Following is the code snippet what I am trying to do.

public class Client{
    private static Map<ApplicationTypeEnum, Connection> connectionPool = new HashMap<>();

    public void init() throws Exception {

        try {
           Connection con1 = getConnection(someparams...);
           connectionPool.put("app-1",con1)
           Connection con2 = getConnection(someparams...);
           connectionPool.put("app-2",con2)
        } catch (Exception pe) {
            throw pe;
        }
    }
}

Now the Connection object are local to init, so is there any chance that the Connectionobjects will be garbage collected when control is out of init method.

3 Answers
Related