Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java : How garbage collection works for static final String object declared in interface and class

I have declared String object inside interface which are by default public, static and final . I want to know does it makes any difference in garbage collection in comparison to class in which the same String object declared with public, static and final

interface Message {
    String SUCCESS = "Request processed successfully";
    String ERROR = "Error in processing your request";
}

class Message {
    public static final String SUCCESS = "Request processed successfully";
    public static final String ERROR = "Error in processing your request";
}
like image 554
Ajit Soman Avatar asked Sep 04 '26 01:09

Ajit Soman


1 Answers

Variables aren't garbage-collected, so it is irrelevant where they are defined. Objects are garbage-collected, but in this case the only visible objects are pooled string literals, and the rules for garbage-collecting those are defined by the JLS and the JVM specification; certainly not by whether references to them are defined in classes or interfaces.

like image 103
user207421 Avatar answered Sep 05 '26 14:09

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!