Jing Hu Jing Hu - 5 months ago 11
Java Question

how to modify value of variable which is be reference in java

A a = new A();
method(A c){
{
//asynchronous network request
c = new A();
//how can i assign c to A,don't use final modifier orstatic modifier
}
}


as above,because of some reasons,I want to assign the value of c to a.And we can not directly assign a = c.In brief,I want a and c point to the same object or address.can anyone help me.Thanks in advance.

all my code is here:

public class LoadResource {
public static String tag = "LoadResource:";
public static int nRightOption;
public static GameInfo gameInfo;
public static Pixmap pixFont;// font
public static Pixmap pixOption[] = new Pixmap[4];// Option pixmap
public static Pixmap imgArray[]=new Pixmap[3];// Detail Pixmap
public static Pixmap imgArray_2[] = new Pixmap[2];//
public static Pixmap historyImg[];// history pixmap

public LoadResource() {
// TODO Auto-generated constructor stub

}

/*
* Loadgame GameInfo
*/
public void startupLoad() {
IGameInfo gameInfo = ServiceGenerator.createService(IGameInfo.class);
Call<GameInfo> call = gameInfo.getGameInfo("15", "84889382", String.valueOf(LevelInfo.currentLevel),
String.valueOf(LevelInfo.currentSubject));
Log.e(tag, tag+"startupLoad");
// request
try {
call.enqueue(new Callback<GameInfo>() {

@Override
public void onFailure(Throwable arg0) {
// TODO Auto-generated method stub
Log.e(tag, "network access failure" + arg0.toString());
}

public void onResponse(Response<GameInfo> arg0, Retrofit arg1) {
// TODO Auto-generated method stub
// result
if (arg0.isSuccess()) {
LoadResource.gameInfo = arg0.body();
Log.e(tag, LoadResource.gameInfo.toString());
if (LoadResource.gameInfo != null&&LoadResource.gameInfo.getDataArray()!=null) {
Log.e(tag, tag + LoadResource.gameInfo.toString());
// loadinfo
nRightOption = LoadResource.gameInfo.getDataArray().getCurrenSelectImg();
// start thread and load pixmap
Log.e(tag,tag+"startupLoadPixmap");
getGamePixmapData();
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}

/*
* start four kinds of thread and load pixmap seperately
*/
public static void getGamePixmapData() {
// font
String strFont = gameInfo.getDataArray().getFontUrl();
// check if exsit in local file,if not exist get pixmap from network
if ((pixFont = pixmapLoader.getPixmap(strFont)) == null)
downloadfile(strFont, pixFont);
// option
String strOption[] = gameInfo.getDataArray().getSelectImgs().split(",");// option pixmap
pixOption = new Pixmap[strOption.length];
for (int i = 0; i < strOption.length; i++) {
if ((pixOption[i] = pixmapLoader.getPixmap(strOption[i])) == null)
downloadfile(strOption[i], pixOption[i]);
}
// detail pixmap
String strDetailPixmap[] = gameInfo.getDataArray().getImgArray().split(",");
imgArray = new Pixmap[strDetailPixmap.length];
for (int i = 0; i < strDetailPixmap.length; i++) {
if ((imgArray[i] = pixmapLoader.getPixmap(strDetailPixmap[i])) == null)
downloadfile(strFont, imgArray[i]);
}
// history pixmap
String strHistoryPixmap[] = gameInfo.getDataArray().getHistoryImg().split(",");
historyImg = new Pixmap[strHistoryPixmap.length];
for (int i = 0; i < strHistoryPixmap.length; i++) {
if ((historyImg[i] = pixmapLoader.getPixmap(strHistoryPixmap[i])) != null)
downloadfile(strFont, historyImg[i]);
}
}
/*
* download file and save to file and static variable @param url:String @param pixmap:Pixmap
*/
public static void downloadfile(String url, Pixmap pixmap) {
IGameInfo gameInfo = ServiceGenerator.createService(IGameInfo.class);
Call<ResponseBody> call = gameInfo.downloadFile(url);
call.enqueue(new LoadResource.downloadToPixmap(pixmap));
}

private static class downloadToPixmap implements Callback<ResponseBody> {
private Pixmap pixLoad;

public downloadToPixmap(Pixmap pixLoad) {
super();
this.pixLoad = pixLoad;
}

@Override
public void onFailure(Throwable paramThrowable) {
// TODO Auto-generated method stub
Log.e(tag, "network access failure" + paramThrowable.toString());
}

@Override
public void onResponse(Response<ResponseBody> paramResponse, Retrofit paramRetrofit) {
// TODO Auto-generated method stub
if (paramResponse.isSuccess()) {
try {
Log.e(tag, "successLoad");
final byte bytes[] = (paramResponse.body()).bytes();
pixLoad = new Pixmap(bytes, 0, bytes.length);
if(imgArray[0]!=null) Log.e(tag, tag+"loadImgArray");
if(pixLoad!=null) Log.e(tag, "successLoadPixmap");
// put in memory and file
InputStream inStream = paramResponse.body().byteStream();
pixmapLoader.putPixmapToMemory(paramResponse.raw().request().url().toString(), pixLoad);
pixmapLoader.putPixmapToFile(inStream, paramResponse.raw().request().url().toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}


just as above my variable pixmap transmited as argument in downloadfile ,can not get new Pixmap() in downloadToPixmap.so how can I achieve it.

Answer

You can't pass arguments by reference, but you can use a mutable wrapper as a workaround. Here's the basic idea:

class Wrapper<T> {
    T value;
}

void method(Wrapper<A> c) {
    c.value = new A();
}

void foo() {
    Wrapper<A> a = new Wrapper<>();
    method(a);
    // a.value is now updated
}