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";
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With