The primary way you can simulate passing a reference is to pass a container that holds the value. static void makeAThree(Reference
ref) { ref.set(3); } public static void main(String[] args) { Reference myInt = new Reference<>(4); makeAThree(myInt); System.out.println(myInt.get()); } Since in Java, it is references to objects that are passed by value (the object itself is never passed at all), setting ref to 3 in makeAThree changes the same object referred to by myInt in main(). Disclaimer: Reference isn't a class you can just use with out-of-the-box Java.
Read full article from How can I simulate pass by reference in Java? - Stack Overflow
No comments:
Post a Comment