blekione
blekione

Reputation: 10590

Difference in resource closing

When I open resource in main method

WebClient webClient = new WebClient();

Eclipse shows me warning Resource leak: 'webClient' is never closed. So I need to close resource by

webClient.close().

But when I get WebClient from method

WebClient webClient = getWebCLient()

public static WebClient getWebClient() {
    WebClient webClient = new WebClient();
    ...
    return webClient;
}

I'm not receiving such an warning. Why?

Upvotes: 0

Views: 57

Answers (1)

Spotted
Spotted

Reputation: 4091

The IDE isn't smart enough to warn you in this case. By no way it means that the WebClient has not to be closed anymore.

If WebClient implements AutoCloseable consider, as a good practice, to wrap the call to getWebClient() into a try-with-resource for automatic closing

try (WebClient wc = getWebClient()) {
    ...
} 

Upvotes: 1

Related Questions