English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

instance variable live the life of the object. But static (nonconstant) variables, when do they restart or "die"?

When the program using the class dies?
when the compiler closes?
when the computer shuts down?

and what would be a good example of where to use a (non-constant) static variable in a class (object) definition.

thanks.
(please, no "try Google" answers)

2006-12-31 07:38:02 · 3 answers · asked by XM 2 in Computers & Internet Programming & Design

3 answers

when the last instance of the class declaring the static variable is destroyed, the variable goes with it. This is done by the automatic garage collection built into java. If an instance of the class is used during the entire process of the program, the static variable is there until the program is terminated.

A good example is to use it with a variable that is used throughout your program that you don't want to have to redeclare.
simple example but illustrates my point: public static String AUTHOR = "Bob Joe"

2006-12-31 07:52:20 · answer #1 · answered by Brady 3 · 0 0

an somewhat final variable can purely be assigned as quickly as.you may't substitute cost of an somewhat final variable. and its unlike consistent, the fee of persevering with is often standard at assemble time however the fee of an somewhat final variable isn't inevitably standard at assemble time. Static variables are class variables.A Static variable would be accessed rapidly with the class, without the would desire to create an occasion of the class.

2016-12-11 19:59:00 · answer #2 · answered by ? 4 · 0 0

Once you compile, the compiler is a non-issue and independent of the program. Static Variables, it is my understanding, allocate storage space when the program is first executed, and that space remains allocated throughout - until the program terminates.

2006-12-31 07:46:40 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers