
-----------------------------------
Martin
Tue Jul 19, 2005 3:30 am

Java 'forgetting' about my class instances
-----------------------------------
I'm having some trouble with a block of code


class ExprOpExpr extends Expr
{
    int value;
    Expr left, right;
    String oper;

    public ExprOpExpr (String op, Expr a, Expr b)
    {
        value = 0;
        if (a == null)
            System.out.println ("a == null");
        left = a;
        right = b;
        oper = op;
    }

    public int getValue()
    {
        if (left == null)
        {
            return -1;
        }
        ....more code...
    }
}

This is part of the creation of an abstract syntax tree. On the way down the tree, while I generate it, a and b are defined (System.out.println ("a == null"); is never called). However, on the way back up the tree where getValue() is called, left and right == null, and I can't figure out why.

Thanks in advance for any help.

-----------------------------------
wtd
Tue Jul 19, 2005 11:47 am


-----------------------------------
Since they're objects and thus being passed by reference, is it possible they get nullified after the constructor is called?

-----------------------------------
Martin
Tue Jul 19, 2005 12:50 pm


-----------------------------------
I don't think so, but how would I make copies of them?

-----------------------------------
rizzix
Tue Jul 19, 2005 8:50 pm


-----------------------------------
i think you have a logic problem that nullifies ur a and b objects somewhere down the line..

to make copies you are first required to implement the Cloneable inteface to the Expr class.. you should then override and implement the protected void clone(); method in that class (as inherited from the Object class) but override it as public, then in the client code above you simply call .clone();

PS: if you are fine with a "shallow copy" of the object you can just as well put the following line in the body of the clone() method: return super.clone();

-----------------------------------
Martin
Wed Jul 20, 2005 7:16 pm


-----------------------------------
Figured it out. Me being stupid. Thanks for the help.
