a strang problem when i change a globe variable's value

D

David

i have two class, in the second one, i use sUpper to locate the insert
point,

class ReliableDatagramSocket extends DodgyDatagramSocket {
private final int WINDOWSIZE=8;
private int sLower=0, sUpper=0, sActive=0;

private void putIntoList(int sUpper, byte[] buf){
sendBuf.add(sUpper,buf);
sUpper=(sUpper+1)%WINDOWSIZE;
}
}

the first one transfers buf to the second one, now the problem is, every
time when i exit from the method, the sUpper return to 0 again, so, i cannot
change the sUpper's value. what's wrong with that???
 
V

VisionSet

David said:
i have two class, in the second one, i use sUpper to locate the insert
point,

class ReliableDatagramSocket extends DodgyDatagramSocket {
private final int WINDOWSIZE=8;
private int sLower=0, sUpper=0, sActive=0;

private void putIntoList(int sUpper, byte[] buf){
sendBuf.add(sUpper,buf);
sUpper=(sUpper+1)%WINDOWSIZE;
}
}

the first one transfers buf to the second one, now the problem is, every
time when i exit from the method, the sUpper return to 0 again, so, i cannot
change the sUpper's value. what's wrong with that???

this.sUpper=(sUpper+1)%WINDOWSIZE;

try that, you were only altering the methods local variable (sUpper).
Alternatively call your local variable something different.
 
R

Ryan Stewart

David said:
i have two class, in the second one, i use sUpper to locate the insert
point,

class ReliableDatagramSocket extends DodgyDatagramSocket {
private final int WINDOWSIZE=8;
private int sLower=0, sUpper=0, sActive=0;

private void putIntoList(int sUpper, byte[] buf){
sendBuf.add(sUpper,buf);
sUpper=(sUpper+1)%WINDOWSIZE;
}
}

the first one transfers buf to the second one, now the problem is, every
time when i exit from the method, the sUpper return to 0 again, so, i cannot
change the sUpper's value. what's wrong with that???

Do a little research on variable scope. You have two sUpper's in your
code. One is an instance variable, meaning it's associated with an
instance of a class. That's the one you declare on the third line. The
other is a method parameter, which is created upon invoking the
method. That's the one on the fifth line. According to Java's rules,
your second declaration of sUpper shadows your first declaration,
meaning that within your putIntoList() method, any reference to sUpper
goes to the second one, the one declared on the fifth line. To
reference the instance variable from within the method, use
"this.sUpper".
 
H

hmtsvs

David said:
i have two class, in the second one, i use sUpper to locate the insert
point,

class ReliableDatagramSocket extends DodgyDatagramSocket {
private final int WINDOWSIZE=8;
private int sLower=0, sUpper=0, sActive=0;

private void putIntoList(int sUpper, byte[] buf){
sendBuf.add(sUpper,buf);
sUpper=(sUpper+1)%WINDOWSIZE;
}
}

the first one transfers buf to the second one, now the problem is, every
time when i exit from the method, the sUpper return to 0 again, so, i cannot
change the sUpper's value. what's wrong with that???

this.sUpper=(sUpper+1)%WINDOWSIZE;
 
D

David

thanks, i get it.
hmtsvs said:
"David" <[email protected]> wrote in message
i have two class, in the second one, i use sUpper to locate the insert
point,

class ReliableDatagramSocket extends DodgyDatagramSocket {
private final int WINDOWSIZE=8;
private int sLower=0, sUpper=0, sActive=0;

private void putIntoList(int sUpper, byte[] buf){
sendBuf.add(sUpper,buf);
sUpper=(sUpper+1)%WINDOWSIZE;
}
}

the first one transfers buf to the second one, now the problem is, every
time when i exit from the method, the sUpper return to 0 again, so, i cannot
change the sUpper's value. what's wrong with that???

this.sUpper=(sUpper+1)%WINDOWSIZE;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top