Change constructor arguments runtime

Hello I don't know how to actually look for this but I am curious, can you change an object based on the arguments you have provided in its constructor. Like you have an object Library which has a constructor public Library(Map books) or something. If I create Map... new Hashmap, put 5 key-values in it, then give that map to the constructor of Library, the Library object will have a hashmap with 5 key-values in it. What will happen if I insert 2 more key-values in the hashmap? Will the Library object which was constructed suddenly have 7 key-values in its map or will it still have just the initial 5?

Answers


It depends on what the constructor does. If the constructor just stores the Map in an instance variable:

public class Library {
    private final Map<String, String> books;

    public Library(Map<String, String> books) {
        this.books = books;
    }

    public int size() {
        return books.size();
    }

    public static void main(String[] args) {
        Map<String, String> myBooks  = new HashMap<String,String>();
        myBooks.put("Jonathan Swift", "Gullivers Travels");
        myBooks.put("Robert Louis Stevenson", "Treasure Island");
        myBooks.put("William Shakespeare", "Hamlet");
        myBooks.put("J R R Tolkein", "The Hobbit");
        myBooks.put("Mary Shelley", "Frankenstein");
        Library library = new Library(myBooks);
        myBooks.put("J K Rowling", "Harry Potter and the Philosophers stone");
        System.out.println("Library contains" + library.size() + " books");
    }
}

Then Library contains a reference to the map passed to it and it will see any changes to the contents of that map. The above code should print that there are 6 books in the library.

On the other hand if the constructor makes a copy of the map:

public class Library {
    private final Map<String, String> books;

    public Library(Map<String, String> books) {
        this.books = new HashMap<String, String>(books);
    }

    public int size() {
        return books.size();
    }

    public static void main(String[] args) {
        Map<String, String> myBooks  = new HashMap<String,String>();
        myBooks.put("Jonathan Swift", "Gullivers Travels");
        myBooks.put("Robert Louis Stevenson", "Treasure Island");
        myBooks.put("William Shakespeare", "Hamlet");
        myBooks.put("J R R Tolkein", "The Hobbit");
        myBooks.put("Mary Shelley", "Frankenstein");
        Library library = new Library(myBooks);
        myBooks.put("J K Rowling", "Harry Potter and the Philosophers stone");
        System.out.println("Library contains" + library.size() + " books");
    }
}

then Library has its own copy of the map and any changes to the original map will not be seen, so the above code should print that there are 5 books in the library.


Yes, it will. For example:

import java.util.Map;
import java.util.HashMap;

public class HelloWorld{

 public static void main(String []args){
    Map<String, String> books = new HashMap();
    books.put("Author1", "Book1");
    books.put("Author2", "Book1");
    Library l1 = new Library(books);
    books.put("Author3", "Book1");
    System.out.println(l1.toString());
 }
}

class Library {
private Map<String, String> books;

public Library(Map<String, String> books) {
    this.books = books;
}

@Override
public String toString() {
    return books.toString();
}

}

The output will be: {Author3=Book1, Author2=Book1, Author1=Book1}

This is because java pass not the object itself to a constructor, but a reference to this object. This means books map from main method and books map from Library are actually one object. If you don't want Library books to change from outside the Library class, you can create new object based on the constructor input. Lie this:

 public Library(Map<String, String> books) {
    this.books = new HashMap(books);
}

Need Your Help

How to allow addon's content script access Javascript variables from the current tab?

javascript firefox-addon firefox-addon-sdk

Supposed my add-on has to access a Javascript variable from a webpage. Let's take https://mozilla.org. There is a global variable called optimizelyCode. My addon will only work if this variable is

Angular2 Hero Tutorial - hero class

angular

In the section where the hero class is temporarily stored on app.component.ts, I'm not sure where to paste the script below where it says we have to refactor.