0

I am java beginner, and the question may seem too basic. I have this code, class named ur it contains some code. below is the code:

public class ur {
       //some code here and object initializations    

     public static {
        new String("AES/CBC/PKCS5Padding");
        new String("someString");
        new String("otherString");
        new String("12");
        new String("16");
        new String("uniqueString");
      }

//some code here

}

What I am asking here is that how the above values is accessed when needed since it was not assigned to a variable. how is this works? or is it useless code?

hanan
  • 532
  • 2
  • 7
  • 23
  • 3
    They can't be accessed which means they will be eligible for garbage-collection. – Pshemo Jun 01 '21 at 14:57
  • 1
    @Pshemo The pointless `new String` instances will be GC'd, but the constants are there for the lifetime of the class. – chrylis -cautiouslyoptimistic- Jun 01 '21 at 15:03
  • @chrylis-cautiouslyoptimistic- will the constants be possible to referenced in the code though? – hanan Jun 01 '21 at 15:05
  • Not with ordinary Java techniques, but they'll still take up memory. – chrylis -cautiouslyoptimistic- Jun 01 '21 at 15:11
  • 1
    “or is it useless code”—mostly. It still can serve for a discussion on Stackoverflow, for example. Note that this is invalid code as what looks like a class initializer here should not have a `public` modifier. You can refer to the constants the same way you refer to them here, e.g. `"otherString"` refers to the same object as the `"otherString"` in the initializer. This demonstrates that it is irrelevant whether the constant appears in the initializer and in fact, whether the object for the constant still exist in memory or not, is an implementation detail. – Holger Jun 01 '21 at 15:12
  • in some cases, I thought that in my opinion, since the constants remain in the memory, there may be a way to be accessed if thier memory address is known. this is guessing but I really wanted to know how is it possible! – hanan Jun 01 '21 at 15:13
  • 1
    To clarify what @chrylis-cautiouslyoptimistic- correctly mentioned. In your code when you write `new String("whatever")` we are facing two string objects. One which is created via `new String` explicitly and second which is String literal like `"whatever"`. If you don't assign anywhere result of `new String` you will not be able to refer to it anymore. But string literals are cached in *string pool* which means each time you write `"whatever"` you are reusing (so maybe we can also say referring to) cached string. – Pshemo Jun 01 '21 at 15:14
  • Perhaps someone can gather together these comments to produce an Answer? – Basil Bourque Jun 01 '21 at 15:36
  • 2
    There is no way to get hands on an object via a memory address, there isn’t even a way to get the memory address in the first place. You can get the reference when you know the string contents, which is trivial, as `"foo"` gives you the reference to the string. [There is a trick](https://stackoverflow.com/a/44929935/2711488) to detect *whether* a particular string existed in the pool prior to the check but it still implies knowledge about the string’s content (and adds the string to the pool as byproduct if it wasn’t already there). You could parse the class file to find the strings but *why*? – Holger Jun 01 '21 at 15:55

0 Answers0