Discussions

General J2EE: References in JAVA

  1. 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)

  2. Re: References in JAVA[ Go to top ]

    So, how would you do it in C?
  3. 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.
  4. 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 @ http://nirav-patel.com/2008/02/27/making-references-unusable/ Please feel free to share your queries/ideas. Cheers, Nirav.
  5. @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...
  6. 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()); } }