12-22-2010 04:42 AM
Hi,
I have a project libA built as a library, including ClassA.java. There's a static string member "arg" defined in ClassA.
I have another project "App" built as a BB application, including a ClassB.java. Import libA into this class, which is success.
However, I then find that I cannot get the correct value of ClassA's static member, "arg". Only the initial value is achieved.
For example, if the initial value of "arg" is "null", then the "ClassB" will achieve this value forever, even it is modified by some other methods after it is defined.
Can anyone help on that? Thanks.
12-22-2010 06:25 AM
hi,
if u define string as static then its value cant be changed because "static" keyword means value cant be changed.so just remove static and try.
thankx
12-22-2010 06:33 AM
Isn't "final" the keyword for that.
12-22-2010 06:39 AM
"static" keyword doesn't mean the value cannot be changed. In fact, I tried to change the value in other methods in libA, and the value can be changed. Just that methods in "app" cannot get the changed value. Strange.
12-22-2010 06:39 AM
hi,
ya its my mistake.
can u just give me ur code.that i can chk it.
thankx
12-22-2010 06:53 AM
Sorry that I don't have the whole code piece at hands now. But I can describe it as simpler.
libA.jar include Class A and Class B .
package libA;
class A
{
public static int num = 5;
//Constructor
A()
{
setNum();
}
public static int num = 5;
public static void setNum()
{
num = 10;
}
public static int c
{
return num;
}
}
class B extends A
{
B()
{
super();
System.out.println(getNum()); // =10
}
}
--------------------------------------------------
package app;
import libA.*;
class C
{
//There's a intialized instance of B already.
int num = B.getNum();
System.out.println(num); // =5 (why isn't it = 10?)
}
12-22-2010 06:58 AM
12-22-2010 07:40 AM
I tried to get the static member in a seperate thread other than that initialized it. So I guess there must be some synchronization problem...
12-22-2010 09:20 AM
hi,
you are extending classA from classB and classC.
so if i did understand u right then see the both code below.
public class ClassA
{
public static int num=5;
public ClassA()
{
setNum();
}
public void setNum()
{
num=10;
}
public int getNum()
{
return num;
}
}
class ClassB extends ClassA
{
public ClassB()
{
super();
System.out.println("From B"+getNum());
//not called
}
public int getNums()
{
return getNum();
}
}
class ClassC extends ClassA
{
public ClassC()
{
super();
System.out.println("From C"+getNum());
//here i got the 10
}
}
and the second is
public class ClassA
{
public static int num=5;
public ClassA()
{
setNum();
}
public void setNum()
{
num=10;
}
public int getNum()
{
return num;
}
}
class ClassB extends ClassA
{
public ClassB()
{
super();
System.out.println("From B"+getNum());
//here i got 10
}
public int getNums()
{
return getNum();
}
}
class ClassC extends ClassB
{
public ClassC()
{
super();
System.out.println("From C"+getNum());
//here i got 10
}
}
thankx
12-22-2010 09:05 PM
Thanks for your replying.
In fact, Class C doesn't extend Class A. It is in the same package with Class B(which extends Class C).
And Class C is a seperate thread instance.
After some experiment, I found that only I invoke SetNum() in Class C, and then its GetNum() will return 10, otherwise it will always return 5. (Even if after sleeping quite amount of time.)
And I'm sure the num is changed to 10 already.