References in JAVA (5 messages)Hi guys, This might be a very basic question, but i have a serious requirement for this kind of implementation. First have a look at the code snippet here. public class Main { public static void main(String[] args) { TIBCOClient tc = new TIBCOClient(); System.out.println(tc); TIBCOClient tc1 = tc; System.out.println(tc1); tc1 = null; System.out.println(tc); System.out.println(tc1); } } ----- Here is my question When i set tc1 to null, i need way that tc also becomes null. Is there any way. If its C or C++ i would have done this easily, but in java :(
Threaded Messages (5)
- Re: References in JAVA by Stefan Zobel on February 13 2008 18:02 EST
- Re: References in JAVA by Santiago Nunez Sanchez on February 18 2008 05:10 EST
- Proposed solution... by Nirav Patel on February 27 2008 05:14 EST
- @Kumar by Abhijit Kulkarni on February 27 2008 07:19 EST
- Re: References in JAVA by Assen Kolov on March 03 2008 11:54 EST
Re: References in JAVA[ Go to top ]
So, how would you do it in C?
- Posted by: Stefan Zobel
- Posted on: February 13 2008 18:02 EST
- in response to KR Kumar
Re: References in JAVA[ Go to top ]
I can't understand why you intend to do this. Obviously you can achieve that tc be null if you set it to null, but you cannot make it directly because '=' is just a right-to-left assignment. Therefore the right operand is not affected by the changes in the left one. Besides, keep in mind that reference values are memory positions in nature, thus a soft of integer values. Consider this example (with all the necessary cautions): int s1 = 4; By setting s1 to 0 later, it does not imply that the previous value (4) is set to 0 too. I hope this can help you.
- Posted by: Santiago Nunez Sanchez
- Posted on: February 18 2008 05:10 EST
- in response to Stefan Zobel
Proposed solution...[ Go to top ]
I tried to comprehend your problem on larger context and thought over possible solution. I've posted it on my blog @ Please feel free to share your queries/ideas. Cheers, Nirav.
- Posted by: Nirav Patel
- Posted on: February 27 2008 05:14 EST
- in response to Santiago Nunez Sanchez
@Kumar[ Go to top ]
Hi Kumar, What you are looking for is memory address of the target of tc reference; and it needs to be set it null so that all referencees (tc1, tc2 ..tcn) pointing to that memory address would refer to null.(Obviously we can have it in C thr ptr) but in java i don't hink there is any through with we can refere it. Also like C++, java doen't support operaor overloading ( we have pre processor implimentation of + for String but not operator overloading) so i don;t think there is any way u can achive it... can u think of handling this situation programtically? no idea.. can u invoke C native function? but need to see if it shares the same prcess memory...i don't have much idea on this. just a pointer .. will let u know incase i find anything on this...
- Posted by: Abhijit Kulkarni
- Posted on: February 27 2008 07:19 EST
- in response to KR Kumar
Re: References in JAVA[ Go to top ]
Hi Kumar, I don't think you can do this in Java, and if you could (say by fiddling the JVM), it would be extremely tough for the next Java developer looking at the code. If you need one reference shared through several variables, why not do exactly this in Java like: public class Main { public static void main(String[] args) { TIBCOClientRef tc = new TIBCOClientRef(); System.out.println(tc.getTibcoClient()); TIBCOClientRef tc1 = tc; System.out.println(tc1.getTibcoClient()); tc1.clearRef(); System.out.println(tc.getTibcoClient()); System.out.println(tc1.getTibcoClient()); } }
- Posted by: Assen Kolov
- Posted on: March 03 2008 11:54 EST
- in response to KR Kumar