Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

How do I swap object references in java?

need to know how to do this for a friend, he said he searched for a tut but couldn't find anything he needed to do so, let me know!
[Image: 4DjkBBj.jpg]
Code
Object example = new Object(); // old reference
example = new Object(); // new reference
(Sep 26, 2016 11:06 AM)Jire Wrote:  
Code
Object example = new Object(); // old reference
example = new Object(); // new reference


Code
Object tempObj = obj1;
obj1 = obj2;
obj2 = tempObj;
System.out.println("Objects switched");

he tried this but it wont work showing an as example
[Image: 4DjkBBj.jpg]
(Sep 26, 2016 11:16 AM)Мишка Wrote:  
Code
Object tempObj = obj1;
obj1 = obj2;
obj2 = tempObj;
System.out.println("Objects switched");

he tried this but it wont work showing an as example

That's because he's making references to an object and variables that don't exist; should be:
Code
public static void main (String[] args) {
Object tempObj = new Object();
Object obj1 = new Object();
Object obj2 = new Object();
obj1 = obj2;
obj2 = tempObj;
System.out.println("Objects switched");
}
class Object {
public void Object() {
...code here
}
}
I don't middleman any deals on- or off-site. Please don't message me requesting my past services and certainly don't fall for anybody who claims to be me.
(Sep 26, 2016 11:36 AM)minimalist Wrote:  That's because he's making references to an object and variables that don't exist; should be:
Code
public static void main (String[] args) {
Object tempObj = new Object();
Object obj1 = new Object();
Object obj2 = new Object();
obj1 = obj2;
obj2 = tempObj;
System.out.println("Objects switched");
}
class Object {
public void Object() {
...code here
}
}

The Object class already exists (java.lang.Object), so you don't need another class for it.

But OP, you provided the correct solution yourself.
thank you guys for your help! appreciate it :)
[Image: 4DjkBBj.jpg]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by LuunaJan 21, 2022 10:13 AM 0 114 Jan 21, 2022 10:13 AM
Last Post: Luuna
[Python] by CryptJan 19, 2021 10:40 AM 11 384 Jan 19, 2021 10:40 AM
Last Post: ThinWhiteDuke
[Java] by deadsh0tJan 10, 2019 07:30 AM 2 170 Jan 10, 2019 07:30 AM
Last Post: deadsh0t