+ 1

Printing out the object reference name? - Java

Hello, I am wondering, how can I print out the object reference names, for example: //objects Student Sam = new Student(); Student John = new Student(); Student Kay = new Student(); //outputting System.out.println("This student is: "+(objectReferenceName)); //? //Desired output This student is: Sam This student is: John This student is: Kay

6th Aug 2017, 1:57 PM
YodaCoda
YodaCoda - avatar
4 odpowiedzi
+ 3
Excellent question (Assuming I'm on the right track), this isn't explained in Sololean's course, but can be a very useful trick. Inside any Object you can create a method called toString(), this is a special method. This method will be called automatically if you try to print an Object. As a result, you're printing the Objects toString(). More advanced info: (If this is confusing, ignore it) toString() is called because of how printing works. Before printing an Object print(Object) will cast your Object to a String using String.valueOf(), which will then use the Objects toString. If there is no toString() then by default the className and hashCode will be printed (Basically the Reference). Here's an example/ public class Program{ public static void main(String[] a){ Animal test = new Animal("Frog"); } } class Animal{ private String type; Animal(String type){ this.type = type; } //This is toString @Override public String toString(){ // Must be public // Must return a String return "The Animal is a "+name; } } Now if I were to print the object: System.out.println(test); Output: The Animal is a Frog So, if you want to print all your Students at once like you wrote, you would need to make another class that stores all the Students. You can call it StudentList. Inside StudentList have a list containing all the students. OR, maybe just use an ArrayList. Then, have the toString() method go through and return the toString() of all the students. The toString in the students can be something like: public String toString(){ return "This student is"+name; } Here's an example code encase you're still confused: https://code.sololearn.com/ckOZ8zsOlYbK/?ref=app Practical Example: (I used toString alot here) https://code.sololearn.com/cZKcrU0TrMtX/?ref=app
6th Aug 2017, 3:24 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
what if you write the following Student Sam = new Student (); Student John = new Student (); Sam = John; then what should your output call print? both John and Sam are references to the same Student object now. Actually you are asking to get information about references and not the object itself that is referenced. I dont think its possible and I dont see the usage. can you give an exemple? in my opinion the way to do this is too set a name attribute to the Student object.
6th Aug 2017, 2:31 PM
Roland Sterkendries
Roland Sterkendries - avatar
+ 1
Coffee OP. Here's another example to hopefully make things easier, maybe I went into to much detail: // pretend this is the main An b = new An(); System.out.println(b); class An{ @Override public String toString(){ return "This was printed!"; } } Output: This was printed!
8th Aug 2017, 5:29 PM
Rrestoring faith
Rrestoring faith - avatar
0
My head hurts. thanks for explaining. Coffee break time for me.
8th Aug 2017, 3:56 PM
YodaCoda
YodaCoda - avatar