Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
babyblue_lee
Posts: 130
Registered: ‎08-10-2010
My Device: Not Specified

Some problems with importing a library

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.

Developer
trainee
Posts: 237
Registered: ‎05-07-2010
My Device: 8520

Re: Some problems with importing a library

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

___________________________________________________
please press kudos(star button on left) if this post is helpful to you
"Accept as solution" if your query has been resolved.@hitendra
_________________________________________________
Regular Contributor
mihaizzav
Posts: 55
Registered: ‎08-26-2010
My Device: 8520

Re: Some problems with importing a library

Isn't "final" the keyword for that.

Trusted Contributor
babyblue_lee
Posts: 130
Registered: ‎08-10-2010
My Device: Not Specified

Re: Some problems with importing a library

"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.

Developer
trainee
Posts: 237
Registered: ‎05-07-2010
My Device: 8520

Re: Some problems with importing a library

hi,

ya its my mistake.

can u just give me ur code.that i can chk it.

 

 

thankx

___________________________________________________
please press kudos(star button on left) if this post is helpful to you
"Accept as solution" if your query has been resolved.@hitendra
_________________________________________________
Trusted Contributor
babyblue_lee
Posts: 130
Registered: ‎08-10-2010
My Device: Not Specified

Re: Some problems with importing a library

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?)
}

Trusted Contributor
babyblue_lee
Posts: 130
Registered: ‎08-10-2010
My Device: Not Specified

Re: Some problems with importing a library

package libA; class A { public static int num = 5; //Constructor A() { setNum(); } public static void setNum() { num = 10; } public static int getNum() { 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?) }
Trusted Contributor
babyblue_lee
Posts: 130
Registered: ‎08-10-2010
My Device: Not Specified

Re: Some problems with importing a library

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...

Developer
trainee
Posts: 237
Registered: ‎05-07-2010
My Device: 8520

Re: Some problems with importing a library

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

 

 

___________________________________________________
please press kudos(star button on left) if this post is helpful to you
"Accept as solution" if your query has been resolved.@hitendra
_________________________________________________
Trusted Contributor
babyblue_lee
Posts: 130
Registered: ‎08-10-2010
My Device: Not Specified

Re: Some problems with importing a library

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.