A question about static data members

So I have this class call DocReader and sets some information it recieves to a data member. Except DocReader is used by a bunch of other classes to. So one classes uses DocReader to get the name of the user, but when I try to use a different class to the same data member(which is a string), it declares it as null.

Could this all be solved is the data member was made static so different classed can access the same data member in that one class DocReader?

[470 byte] By [blackmagea] at [2007-11-27 4:54:10]
# 1
do you have seperate class files. Remember you can only have one upper Static Class.red
SandyReda at 2007-7-12 10:08:46 > top of Java-index,Java Essentials,New To Java...
# 2

If your two different classes that use DocReader have the same reference to DocReader, then they access the same data. But if you're instantiating DocReader twice -- once in each of the two classes that use it -- then they will be accessing different objects and thus different information.

So in summary you don't need to make it static (although this will work), you just need both classes that use it to have the same reference.

ktm5124a at 2007-7-12 10:08:46 > top of Java-index,Java Essentials,New To Java...
# 3

How are boths classes using it as the same references.

Like I have

public class UserWindow{

DocReader doc=new DocReader();

doc.setUser("Fred");

}

and then

public class SearchForUser{

DocReader doc=new DocReader();

doc.getUser("Fred");

}

for the second one, should is just have

public class SearchForUser{

DocReader doc;

doc.getUser("Fred");

}

bc when I do that, I get a NullPointerException e

blackmagea at 2007-7-12 10:08:46 > top of Java-index,Java Essentials,New To Java...
# 4
That's because you never initialized doc. You're making a method call on a null reference. You need to pass the reference to doc to the different objects.
ktm5124a at 2007-7-12 10:08:46 > top of Java-index,Java Essentials,New To Java...
# 5
But I meant, how do you make them reference the same object?
blackmagea at 2007-7-12 10:08:46 > top of Java-index,Java Essentials,New To Java...
# 6
You pass the reference through a method or constructor. How you do it is a matter of design.
ktm5124a at 2007-7-12 10:08:46 > top of Java-index,Java Essentials,New To Java...