will 'count' variable be garbage collected?

hi guys

need some help, i want to know if 'count '  variable in below code be garbage collected?

My program structure is >>Adapter.java calls TemplateAdapter.java.
count is defined as a global variable in TemplateAdapter.java
here is the code

Adapter.java

public Workflow transformDTOToDbModel(WorkflowDTO workflowdto){
Workflow workflow = new Workflow();      
workflow.setId(workflowdto.getId());      
workflow.setTemplate(TemplateAdapter.getInstance().populateQuestionChoices(workflowdto.getQuestionChoicesDTO()));
//populate the % complete
int questionsanswered = TemplateAdapter.count;            
double percentcompl = questionsanswered / 30.0 * 100;
int  percentageComplete = percentcompl.intvalue
workflow.setPercentComplete(percentageComplete);
return workflow;
}

public class TemplateAdapter implements ITransformer<Template, TemplateDTO> {
public static int count = 0;
private static TemplateAdapter _instance;
private Logger logger = Logger.getLogger(getClass());
      
private TemplateAdapter(){}
public static TemplateAdapter getInstance(){
if(null == _instance)
_instance = new TemplateAdapter();
return _instance;
}
      

public QuestionChoices populateQuestionChoices(QuestionChoicesDTO questionChoicesDTO)
{
QuestionChoices questionChoices = new QuestionChoices();  
List<Answer> answerList = new ArrayList<Answer>();              
for (AnswerDTO answerDTO : questionChoicesDTO.getAnswers())
  {
  answerList.add(populateAnswers(answerDTO));
  }                
  questionChoices.setAnswers(answerList);              
  return questionChoices;
}      
 
public Answer populateAnswers(AnswerDTO answerDTO)
{
  Answer answer = new Answer();  
  answer.setAnswerValue(answerDTO.getDescription());
  if (answer.getAnswer() != null && answer.getAnswer().trim().length() > 0) {
count++;
  }
  return answer;
}            
      
}

Will count take a dump and cause garbage?

thanks      
royjaydAsked:
Who is Participating?
 
for_yanCommented:
count is static variable and will be garbage colleced only when the class is unloaded

Static variables are referenced by Class objects which are referenced by ClassLoaders -so unless either the ClassLoader drops the Class somehow (if that's even possible) or the ClassLoader itself becomes eligible for collection (more likely - think of unloading webapps) the static variables (or rather, the objects they reference) won't be collected.

0
 
for_yanCommented:
http://stackoverflow.com/questions/453023/are-static-fields-open-for-garbage-collection


      

Static variables cannot be elected for garbage collection while the class is loaded. They can be collected when the respective class loader (that was responsible for loading this class) is itself collected for garbage.
0
 
for_yanCommented:
If you use not static but instance variable, then it will be garbge collected when the instance which contains it
itself becomes eligible for garbage collection.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
for_yanCommented:
So in case of normal java application you shopuld assume that static variables will llive through the life of your application.
0
 
royjaydAuthor Commented:
Also i just realized i cannot make the above code work unless i make the populateAnswers() method static.
My second question would be how can i access the correct count so that i can
populate the % complete and at the same time i dont want count to cause garbage.

0
 
for_yanCommented:

I'm not sure I understand - you can use static variables in the non-static method - it is not a problem
0
 
royjaydAuthor Commented:
>>>you can use static variables in the non-static method - it is not a problem
 i thought static variables can be used only in static methods?
0
 
for_yanCommented:
No, it is opposite: in static methods you cannot use instance varaibles
0
 
for_yanCommented:
static variables can be used everywhere even in instance of another class, just referencing this class name
0
 
for_yanCommented:
And if I were you, I would worry the least about grabge collection especuially of such thing
as one integer variable - that is jvm business and it will do it for you
0
 
for_yanCommented:
With static variables you should only worry if this class may be simpulatenousely accessed
by say differernt clients - in this case they may all share the same static variable.
If nothing like that can happen, then it is fine. If diiferent clinets access the same class, then
you should realize what it is that you are counting.
These considerations is good to have in mind and understand; they are more imporatnt than
garbage collection which will happen as it should without us needing to worry about it.
0
 
for_yanCommented:
And the rule that
static methods you cannot use instance varaibles
is easy to understand - static method is not specific to any instance -
thisnk of it as just general description of some procedure - it
the result of it should denpend only on the imnput parameterts -
and you call it refreing only to the class in general, not refrring to any instance of this
class, and instance variable is pspecific to particular insatnce - makes no sense to use it
in general for the whole class static method.

And all that does not apply to your case - in the opposite situation there is no
limitation - you can uses staatic variables  in instance methods as much as you want -
you can for instance yuse them to calculate how many times this or that instance method of this
was called overall by all instances exsitinc in your applaications.
OK, this is all probably beyond your question, but it is good to realize all that,
so as you have doubtes i decided to explain.
0
 
objectsCommented:
Its got nothing to do with static. count is a primitive variable.
They have nothing to do with garbage collection which Objects
So no count is never going to get garbage collected
0
 
for_yanCommented:
If count is instance variable of the class it will become no longer accessible
as soon as the instance of this class is garbage collected
0
 
royjaydAuthor Commented:
primitive variables dont have to worry about garbage collection? so int/double/ other primitive data types dont have to worry about GC?

0
 
for_yanCommented:
In most ordinaly programs you don't have to worry about garbage collection anyway.
That's the advantage of java, that it takes care of garbage collection for you.
This is a good subject for the question in Java classes, and maybe in some
very special cases where memory becomes an issue. In my practice don't recall
having such case in years where I had to think of garbage collection.
0
 
objectsCommented:
> primitive variables dont have to worry about garbage collection? so int/double/ other primitive data types dont have to worry about GC?

no, they aren't stored on the heap so theres no garbage collection to do.
The fact that the variable is static is irrelevant
0
 
royjaydAuthor Commented:
so all the primitives are stored on the stack?
thx.
0
 
objectsCommented:
sorry was a bit unclear with that, they are held on the heap but as a part of the containing class.
It would not however get garbage collected independently like a non-primitive variable would.
0
 
royjaydAuthor Commented:
>>>It would not however get garbage collected independently like a non-primitive variable would.
so you are saying String (non-primitive) would be garbage collected and int would not be garbage collected?
can you give an example to show what you are saying

thx

0
 
objectsCommented:
class X {
    String a = "abc";
    int b = 9;
}

X x = new X();
x.a = null;

// a is now available for garbage collection

x = null;

// x is now available for garbage collection
// at which time b will also get freed up
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.