Private Java class properties mysteriously reset between method calls....
Posted
by Michael Jones
on Stack Overflow
See other posts from Stack Overflow
or by Michael Jones
Published on 2010-05-14T14:21:00Z
Indexed on
2010/05/14
14:24 UTC
Read the original article
Hit count: 298
I have a very odd problem.
A class property is mysteriously reset between method calls.
The following code is executed so the constructor is called, then the parseConfiguration method is called. Finally, processData is called.
The parseConfiguration method sets the "recursive" property to "true". However, as soon as it enters "processData", "recursive" becomes "false".
This problem isn't isolated to a single class -- I have several examples of this in my code.
How can this possibly be happening? I've tried initialising properties when they're declared outside any methods, I've tried initialising them in constructors... nothing works.
The only complication I can think of here is that this class is invoked by an object that runs in a thread -- but here is one instance per thread, so surely no chance that threads are interfering. I've tried setting both methods to "synchronized", but this still happens.
Please help!
/**
* This class or its superclasses are NOT threaded and don't extend Thread
*/
public class DirectoryAcquirer extends Manipulator
{
/**
* @var Whether to recursively scan directories
*/
private boolean recursive = false;
/**
* Constructor
*/
public DirectoryAcquirer()
{
}
/**
* Constructor that initialises the configuration
*
* @param config
* @throws InvalidConfigurationException
*/
public DirectoryAcquirer(HierarchicalConfiguration config) throws InvalidConfigurationException
{
super(config);
}
@Override
protected void parseConfiguration() throws InvalidConfigurationException
{
// set whether to recurse into directories or not
if (this.config.containsKey("recursive"))
{
// this.recursive gets set to "true" here
this.recursive = this.config.getBoolean("recursive");
}
}
@Override
public EntityCollection processData(EntityCollection data)
{
// here this.recursive is "false"
this.logger.debug("processData: Entered method");
}
}
© Stack Overflow or respective owner