I missed class the other day. I'm lucky to have a professor who posts class notes for the day, everyday. In the code she posted for the day of class I missed, she seemingly declared an object and assigned it to a method, which confused me. I'm here to ask for help, so I can get an understanding of what she did.
The program stores a user's name and sales for a company in an array and determines whether the sales were either above or below average. I tested the program and everything runs as intended. The program consists of three classes (with one to test).
Here is the code for one of the one of the classes of the program (I have no problems understanding):
The second class uses the first class to create array objects for each person. Here is the code (I commented where I have trouble understanding):
It's not complicated. That code calls the "getHighest" method, which as you can see returns a SalesPerson object. And then it assigns that object to the "highest" variable.
There's no creation of objects going on there at all. And if you look at the details of the getHighest method, you'll see that it doesn't create any SalesPerson objects either. It just returns an existing SalesPerson object as its value.
But you're thinking that if you declare a SalesPerson variable, then that necessarily means a SalesPerson object is created for it, right? No, that isn't how it works. Despite the way I said things just a moment ago, a SalesPerson variable doesn't actually contain a SalesPerson object. Instead it contains a reference to a SalesPerson object. So if you write
that means you're creating a new SalesPerson object -- you know that already -- and you're assigning a reference to that object to the "donald" variable. If you then write
that means you're taking an existing SalesPerson object -- the one which "donald" has a reference to -- and assigning a reference to that existing object to the "trump" variable. Now both variables contain references to the same object.
(If you like to think of references as being pointers, go ahead. That's basically what they are.)
So in the original code you asked about:
the getHighest() method will return a reference to a SalesPerson object, and that reference gets assigned to your "highest" variable. Where the getHighest() method finds that SalesPerson object doesn't matter to this line of code.
I dunno, everybody who learns Java seems to stumble over this point. I'm surprised that teachers don't seem to bother to highlight it, since if you don't understand this point then you're going to be confused by pretty much every piece of non-trivial Java code.
I've listed below what I think are the relevant lines of code in your example.
There is a global array of SalesPerson being kept. (line 5)
Each item in the array is created using the new operator (line 26)
getHighest() does some processing and returns an already created object at line 47.
Line 65 declares a variable and gives it the same value as the already constructed object created at line 26 and returned at line 47.
Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Joined: Oct 14, 2011
Wow. That simple, eh. I now have a greater fundamental understanding of objects and their references.