Pointer-like behavior in Java

Posted by Shmoo on Stack Overflow See other posts from Stack Overflow or by Shmoo
Published on 2010-04-09T13:58:13Z Indexed on 2010/04/09 14:03 UTC
Read the original article Hit count: 109

Filed under:
|
|
|
|

I got the following:

class A{  
    int foo;  
}  
class B extends A{  
    public void bar();  
}

I got a instance of A and want to convert it to an instance of B without losing the reference to the variable foo.

For example:

A a = new A();  
a.foo = 2;  
B b = a; <-- what I want to do.  
//use b  
b.foo = 3;  
//a.foo should now be 3

Thanks for any help!

© Stack Overflow or respective owner

Related posts about java

Related posts about pointer