0

I've made an ArrayList called books, which only contains the class LibraryBook that I've created. And I've created this in another class called Library.

public class Library {

   ArrayList<LibraryBook> books;

   public Library() {
      books = new ArrayList<LibraryBook>();
   }

Then I have a method in this Library class which adds LibraryBooks to this ArrayList.

public void add( String title, String author) {
  LibraryBook b = new LibraryBook( title, author);
  books.add( b);
}

So if I say, in a main method;

Library library = new Library();
library.add( "Title", "Author");

I get a new LibraryBook in my ArrayList. However if I do this again;

Library library = new Library();
library.add( "anotherTitle", "anotherAuthor");

Now I have another LibraryBook in my ArrayList. But in the add method, I've made LibraryBook b to refer to these objects. So what refers to these objects now? Are they both referred by the variable b (which shouldn't happen as far as Java is concerned)? Or they don't have any references at all?

My guessing would be, that when I put an object into an ArrayList, it creates a referrence to this object and stores that instead. And if I put a reference to an object into an ArrayList, it does the same thing.

If this is true, then another question, how does Java let me create two different LibraryBooks that are referred with the same variable b in my add method?

EDIT: I've found another thread with the same title before I made this post, but I didn't quite get the answer I was looking for so here we go.

EDIT2: With all due respect, how is this question any relevant to the linked post? The linked post is so "general" that any post in this forum can be linked to that one and considered as a duplicate. It doesn't hold an answer to my question at all. Even the word ArrayList is not present in there. I would appreciate a feedback on how the two posts are even related other than the fact that the linked post is a general "lesson" for the basics of Java.

  • Once `Library#add` ends, the list will be the only one referencing the object's. The `add` method creates a `LibraryBook` as well as a reference to it. After adding it to the list, the method returns and `b` no longer references the object. – Vince Dec 11 '16 at 16:44
  • to answer the question you'd likely have some form of internal, static cache which tracks `LibraryBook` objects based on input criterium. Though this is usually for more expensive/immutable objects – Rogue Dec 11 '16 at 16:45
  • @VinceEmigh Oh, you're right. Since b is only available in the method add, as soon as it stops running it, b doesn't refer to anything anymore, did I get that right? –  Dec 11 '16 at 16:53

2 Answers2

3

My guessing would be, that when I put an object into an ArrayList, it creates a referrence to this object and stores that instead. And if I put a reference to an object into an ArrayList, it does the same thing.

Almost. When you say something like..

libraryList.add(new LibraryBook("hello", "world"));

You're not actually adding a new object into the list. The new keyword returns the reference of the newly created object, not the object itself. So saying..

LibraryBook b = new LibraryBook("hello", "world");

and adding that to the list is exactly the same. In fact, the compiler will likely remove the unnecessary variable b to save memory allocations during runtime.

If this is true, then another question, how does Java let me create two different LibraryBooks that are referred with the same variable b in my add method?

Good question! To understand this, you need to understand scoping. If I declare a variable inside a function, like so:

public void myMethod() {
    String s = "hello";
    System.out.println(s);
}

When that method exits, the reference (that you've called s) is destroyed. If no other references exist, the garbage collector comes along and destroys the object itself, since nothing refers to it.

This means that if you call myMethod again, a new reference is created. They might share the same block of code but they will exist as two mutually exclusive variables in the system.

In your case, because you're adding objects into your list, your list maintains a reference to the objects and the garbage collector won't remove them, so every time a new b is made when you call the function that has no link to any other version of b that was made when the method was called previously.

christopher
  • 26,815
  • 5
  • 55
  • 89
0

If look at your add method,

public void add( String title, String author) {
  LibraryBook b = new LibraryBook( title, author);

'b' is a method local variable (reference) which will get destroyed, once the method execution gets completed, but the object still will be there on heap. so everytime you call this method, a new object will get created, and reference b will be pointing to the newly created object

    books.add( b);

This line will put the refernce of objects created to arraylist object, objects are always stored in the heap. So whenever you create and object, memory is allocated in heap, and the reference will be stored in the stack segment, and the frame will be taken out of stack, once the method execution finishes. Since books is a class level variable, the reference to those objects will exist till Library object is garbage collected.