Re: How do you implement global data?
- From: Mark Space <markspace@xxxxxxxxxxxxxx>
- Date: Wed, 11 Apr 2007 05:34:03 GMT
Knute Johnson wrote:
I have some data that I need to have accessible to multiple classes. The data is initialized from the constructor of one class by reading a file. That same class has an editor that can modify that data. Currently I'm just creating the variables as static in the one class. Findbugs complains that this is "tricky (which it is) and bad practice". How would you setup shared data under these conditions?
I'm not sure I understand the question completely.
You're saying you read some data from a file and stuff it in a static variable?
class Editor {
static String myData;
public Editor( IOStreamReader file ) {
// read data into myData from file...
}
}
Yes, this is pretty evil. If you need direct access to the data, you should at least go through a getter and a setter. JTextArea has, iirc, a getText and a setText. So if a second class needs to modify myData, it uses the getter, makes changes, then invokes the setter. However in any case I'd ditch the static modifier. It's not needed and will probably produce some really unexpected behavior eventually.
If you're asking something different, I'm not sure what it is. More info?
.
- References:
- How do you implement global data?
- From: Knute Johnson
- How do you implement global data?
- Prev by Date: Job Openings - India
- Next by Date: can you help me in java
- Previous by thread: Re: How do you implement global data?
- Next by thread: Re: How do you implement global data?
- Index(es):
Relevant Pages
|