This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

static variable: can I always get its value ??

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the follwoing test code:
***************************
package dir1;
import dir2.*;
import dir3.*;
public class Class1 {
public static void main(String[] args) {
String id = "abc";
Class4.NUMBER = id;
Class2 class2 = new Class2();
class2.doIt();
}
}
**********************************
package dir2;
import dir3.*;
public class Class2 {
public void doIt() {
Class3 class3 = new Class3();
class3.doIt();
}
}
***************************************
package dir3;
public class Class3 {
public void doIt() {
Class4 class4 = new Class4();
class4.getIt();
}
}
********************************************
package dir3;
public class Class4 {
public static String NUMBER;
public void getIt() {
String newNumber = NUMBER + "def";
System.out.println("newNumber = " + newNumber);
}
}
**************************************
The output is "newNumber = abcdef".
Question: When Class4 instance access its method, is that static NUMBER variable still existing ?
If the Class1,2,3,4 are very long code, could the Class4 have been garbage collected when the program reaches the line of
String newNumber = NUMBER + "def";
in Class4 so that NUMBER is no longer "abc" ?
Is the above code guaranteed to be safe in terms of letting Class4 get the value of "abc" for variable NUMBER ?
Thanks,
Mike
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,
Normally folks worry about keeping things around longer than they need to be but you seem to be worried that you'll lose the value assigned to NUMBER before you get a chance to use it.
Don't worry: Class4 will keep anything assigned to NUMBER. The static NUMBER variable will always exist because it is part of the class (due to the static keyword).
The point of having a garbage collection system is to relieve the programmer from the tedious and error-prone task of managing memory. So again, for the most part, you really shouldn't worry about these kind of issues at all.
Junilu
 
I'm so happy! And I wish to make this tiny ad happy too:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic