• How do I dereference a static string

    From C.D. Altenburg, Ph.D,@21:1/5 to All on Thu Nov 3 10:05:09 2022
    Say I have the string

    public static string foo = "foo";

    can I just do

    foo = null;

    in order to dereference that string?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Leitgeb@21:1/5 to cdalten@gmail.com on Thu Nov 3 17:38:46 2022
    C.D. Altenburg, Ph.D, <cdalten@gmail.com> wrote:
    Say I have the string
    public static string foo = "foo";
    can I just do
    foo = null;
    in order to dereference that string?

    For what you're doing, "unreferencing" might be the better word.

    The word from subject "dereferencing" rather denotes access of the
    referenced thing, like in System.out.println(foo);

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From e.d.programmer@gmail.com@21:1/5 to All on Thu Nov 3 12:37:47 2022
    On Thursday, November 3, 2022 at 1:05:17 PM UTC-4, C.D. Altenburg, Ph.D, wrote:
    Say I have the string

    public static string foo = "foo";

    can I just do

    foo = null;

    in order to dereference that string?
    That depends what you mean by dereference. If you want to clear the value, sure set to null like that would work. If you mean remove the variable from memory, static values are tied to the class, you'd have to unload the class.
    It is still possible but not advisable to have a public static variable which is not final. It's better to make it private static and use accessor methods.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Arne_Vajh=c3=b8j?=@21:1/5 to e.d.pro...@gmail.com on Thu Nov 3 20:14:23 2022
    On 11/3/2022 3:37 PM, e.d.pro...@gmail.com wrote:
    On Thursday, November 3, 2022 at 1:05:17 PM UTC-4, C.D. Altenburg, Ph.D, wrote:
    Say I have the string

    public static string foo = "foo";

    can I just do

    foo = null;

    in order to dereference that string?

    That depends what you mean by dereference. If you want to clear the
    value, sure set to null like that would work. If you mean remove the
    variable from memory, static values are tied to the class, you'd have
    to unload the class.
    Note that:

    ... foo = "foo";
    ...
    foo = null;

    Involves two memory locations.

    The variable foo that is a reference and a
    String object containing "foo".

    Arne

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Arne_Vajh=c3=b8j?=@21:1/5 to All on Thu Nov 3 19:29:49 2022
    On 11/3/2022 1:05 PM, C.D. Altenburg, Ph.D, wrote:
    Say I have the string

    public static string foo = "foo";

    can I just do

    foo = null;

    in order to dereference that string?

    It is a valid statement.

    But note that Java does not use reference
    counting for memory management so setting
    references to null when "done" is
    very rare in Java (doing it when "ready"
    is seen).

    Arne

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)