java全局函数和成员函数,为什么全局变量不会在同一全局变量内用全局函数更新? Android / Java...

For an application in android

I have a global variable,

public class ConstantsUrls {

public static String GET_CART_ALL_ITEM = "store/3/cart/" + CartFunctions.ReturnUserRrQuote() + "/type/customer"

}

and have ,

public class CartFunctions {

public static String ReturnUserRrQuote() {

String user_value = "";

//some function

return user_value;

}

}

When I call a function, let A with parameter ConstantsUrls.GET_CART_ALL_ITEM as

A(ConstantsUrls.GET_CART_ALL_ITEM);

I get the correct value as store/3/cart/100/type/customer but when I call again

the function A with the same parameter I always get exactly the same value as store/3/cart/100/type/customer even the ReturnUserRrQuote() not call to get updated value for the second time.

When I call the function

A("store/3/cart/" + CartFunctions.ReturnUserRrQuote() + "/type/customer")

instead of

A(ConstantsUrls.GET_CART_ALL_ITEM);

I always get the correct working (updated correct values)

Why Global variable does not update with global function within the same Global variable.

Is this Java core behave like this or any other reason?

解决方案

In your code, the constant GET_CART_ALL_ITEM is created and initialize only one time, and it takes the current value of ReturnUserRrQuote(). Later It will not trigger the function as the constant already has its value and does not require a new initialization.

If at the beginning of code ReturnUserRrQuote() => "100". Then GET_CART_ALL_ITEM is created and initialize with this value, it will be "store/3/cart/100/type/customer" and not "store/3/cart/" + ReturnUserRrQuote() + "/type/customer". It's due cause at initialization the expression "store/3/cart/" + ReturnUserRrQuote() + "/type/customer" is evaluated and the result is affected to the constant (the expression is not affected to the constant).

So when you call this constant later, with supposed ReturnUserRrQuote() => "250". GET_CART_ALL_ITEM still is "store/3/cart/100/type/customer". You haven't redefine it to contain the new value of ReturnUserRrQuote() (And you don't want java to do so or it won't be a constant).

In your case either you want a constant so it's normal that it don't change whenever ReturnUserRrQuote() changes. Or you want it to re-evaluate at every time and you do not want a constant. You could do something like :

public static final const1 = "store/3/cart/";

public static final const2 = "/type/customer";

//whenever you have to obtain your value

String value = const1 + ReturnUserRrQuote() + const2;

Edit:

You speak about global variable and not constant but the problem is the same. Even with non global variable.

//Static function that return the number of times it has been called

public static returnNumber() {

final int i=1;

return i++;

}

public static void main() {

int a = returnNumber(); //Initialize a

for (j=0; j<10; j++) {

System.out.println(a); //print the current value of a

}

}

In this example, a will be initialize at the beginning of main. the expression returnNumber() will be evaluated and the result will be affected to a. Here it's the first call to returnNumber then the result is 1. So the value of a is 1 and not returnNumber(). In the loop I call a 10 times and I print it. The 10 times a will value 1 and the number 1 will be printed 10 times. It does not call returnNumber() every time I call a.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值