Can you get the previous value of a variable in Java?

Let's say you have a variable (let String Str say) and the value of str starts as "and then when some code works, it is set to" test "and then somewhere else in the code, it changes again to say “tester.” Now in the program I want to know what the previous value of Str is. Is this possible in Java?

So, I say that a variable changes twice, and you want to know what Str is before it is changed a second time. Thus, in the above example, the last value of Str would be a “tester”, but I wanted to find out what Str was before (if you did not know what it was before it was changed to a tester), in this case I would like to be able to find out that Str was a "test".

Is it possible to do this in Java?

+5
source share
14 answers

No, this is not possible, you need to save the previous value before changing it to do what you are asking for.

+15
source

, . , (?) , .

private String str;
private String prev;

setStr(String s)
{
    prev = str;
    str = s;
}

prev.

, , , str.

, deworde, , , . , IDE.

+11

, .

:

AOP, AspectJ, .

. pointcutJ pointcut

JavaBeans

JavaBean-, , . bean, .

. JavaBean Spec.

:

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;

public class MyBeanListener implements PropertyChangeListener,
        VetoableChangeListener {

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        System.out.printf("Notifed of property changed event: %s => %s%n", evt
                .getOldValue(), evt.getNewValue());

    }

    @Override
    public void vetoableChange(PropertyChangeEvent evt)
            throws PropertyVetoException {
        System.out.printf("Notified of vetoable change event: %s => %s%n", evt
                .getOldValue(), evt.getNewValue());
    }
}
+5

, :

public class Main 
{
    public static void main(final String[] argv) 
    {
        SavedValue<Integer> i;

        i = new SavedValue<Integer>();
        i.set(7);
        System.out.println(i.get());
        System.out.println(i.getOld());
    }
}

class SavedValue<T>
{
    private T oldValue;
    private T value;

    void set(final T val)
    {
        oldValue = value;
        value    = val;
    }

    T get()
    {
        return (value);
    }

    T getOld()
    {
        return (oldValue);
    }
}

, , ? , , , .

+4

:

  • , Java

, :

Aspect AspectJ, . , , , . , , .

, AspectJ Java, , .

, AspectJ CGLIB.

+3

, , ?

Visual Studio , , " " , .

, , . VS2005 :

  • "When Hit...".
  • " "
  • , " " - .

, , ; (ABCDFEGH...)

, do , .

+2

, . String, - .

+1

, , - List, .

, . , . , .

+1

. . java:

Deque<Integer> stack = new ArrayDeque<Integer>();

, , ( setter, AOP, ). , .

+1

, , .

, , LIFO. . , , . , .

. -, , .

, , .

0

, , .

eclipse, , . , , .

0

, -, , -, .

The historic Gooogle Bil Lewis debugger for an example. Please note: most of this software is in what you would call the “initial beta” phase.

0
source

Have you heard of Jiva? This allows you to step backward in debugging, so this will partially answer your question. Read here: http://www.cse.buffalo.edu/jive/

0
source

Yes!! When you change a value, the value changes only for a specific object. See Ex:

 class Test{
int a=100; //original value 
 Test(int b){
    this.a=b;
 }
 Test(){
 }
void print(){
 System.out.println(a);
}
 public static void main(String args[]){
     Test t=new Test(9);
     Test t2=new Test();
     t.print();      //output: 9
     t2.print();     //output: 100 
 }
}
0
source

All Articles