You can easily test this reference stuff yourself
int i;
int j;
i = 3;
j = i;
// print i and j to confirm their values
j = 4;
// print i and j to check their values
//
// If i here also changed, j and i are referencing the same object, else they are different objects
54 minutes ago, Calcious said:
edit: Actually, now that I'm thinking, if an object gets dumped after leaving a function, then how do stuff like save games, etc work? Even if you copy the reference to the main method, it's all going to be dumped after you exit the program right?
Yes. In fact, if you turn off the computer, literally everything in memory is gone (note that hibernating isn't really switching off here, I'll explain how that works a bit further).
You are fully correct, data in memory is very volatile, it lives only as long as the program lives (and perhaps a lot shorter, depending on what references to the data). One obvious solution is to never turn the computer off, but that's a bit unpractical 
That wasn't always the case though, at the very beginning we had magnetic drums https://en.wikipedia.org/wiki/Drum_memory
Computers didn't have memory chips, that drum WAS the memory that was used for running the program. Can you imagine, having a rotating drum next to your computer, literally seeing the computer reading and writing memory? I wasn't born at the time, but wow, talk about physicial computing 
Obviously, this was slow as hell, and people invented computer memory chips https://en.wikipedia.org/wiki/Semiconductor_memory
Much faster access, but it had the problem that you found. When you turn off the power, all its content is gone. This caused all kinds of interesting startup problems, ie after power-on, the computer needs instructions to start, but the memory is empty.
Eventually, this led to creation of ROM (read-only memory) https://en.wikipedia.org/wiki/Read-only_memory
While it solved the start-up problem, it's read-only, ie you cannot write your data to it. That was another journey that started:
Smart people invented offline storage systems. These systems can store new data without needing power to preserve it. At a later date, you can read that data from the storage again, and you can continue working with it.
We had lots of different systems there, it mostly started with paper tape https://en.wikipedia.org/wiki/Paper_tape
(the holes in the paper contain the information, so yep, you can retrieve that many years later, until the paper cumbles or until it is burnt.)
Long lints of paper wasn't very convenient, so they switched to punch cards: https://en.wikipedia.org/wiki/Punched_card_input/output
Problem here was if you dropped a stack of cards on the floor, you had a great time putting them all back in the right order 
Unfortunately, I never worked with the above systems, I would love to do that once, to experience how that worked.
Then people discovered you could use magnetic tape instead, so audio cassettes were used (you could store music on them, so surely you can store digitial information on it) https://en.wikipedia.org/wiki/Compact_Cassette
Professional systems used dedicated tape systems for that https://en.wikipedia.org/wiki/Magnetic_tape_data_storage
Even today, there are tape robots in data storage centers, where you have a bunch of tape readers / writers, and huge number of tapes, and a robot swapping the tapes of the reader. https://en.wikipedia.org/wiki/Tape_library
The reason is that tape storage is extremely cheap and reliable, so if you have a few Terrabyte (or more), it's a good option.
Closer to home, tapes have the problem that if some data is at the other end of the tape, you have to literally rewind the entire tape, which could easily take a minute or more. So people came up with the idea to put the tape on a circular flat surface, with a moving head, you could just move the head a few inches left or right, that'd be much quicker right? It was, and we got floppy disks https://en.wikipedia.org/wiki/Floppy_disk
Worked great, much better than tape. Of course, people are never happy, and wanted more speed, so they made the disk much less 'floppy', and put everything in a clean environment, so the head could hover above the surface at a distance of less than the thickness of a human hair. That dramatically increased the speed, and we got hard disks https://en.wikipedia.org/wiki/Hard_disk_drive
Nowadays, the head movement is a limiting factor, as is the fact that a disk rotates at a finite speed. Worst case, you need to wait a full rotation of the disk before you can read the data. That is being solved with SSD disks https://en.wikipedia.org/wiki/Solid-state_drive
No mechanical parts any more, so way faster. Also, it doesn't need power to move the head or spin the disk, which is great news for battery lifetime.
After this fun but totally unneeded trip through history, back to your question.
The solution to avoid loosing data is to store it onto a storage system, commonly known as "the disk" or "hard disk". In windows, it's called "C:'\" or "D:\" or so. The basic procedure is to open a file on it for writing, write the data you want to preserve, and then close the file. The next time you want that data, you do the reverse. You open the file for reading, read the data, and close the file again.
All this is explained in much more detail in a chapter called "file input / output" in your tutorial.
Finally, I promised to discuss "hibernating". If you start a computer from scratch (a cold start), it takes a long time to get to the point that you can start working with it. People don't want to wait that long, they want to open their laptop, and immediately start working. However, you can't turn off the computer without loosing all its data in memory, so what to do?
The trick they use is as simple as it's elegant. They simply save the entire memory in a big file. Modern hard disks are way bigger than memory space, so this is not a problem at all. They called it "hibernating", but its just "save the entire memory, and turn yourself off". When you open the computer again, it detects that saved file. Instead of starting from scratch, it just loads the entire file back in memory, et voila, back up and running in a few seconds!