TL;DR
In my personal opinion, languages that automatically pass data structures as references without extra effort from the developer should be classified as pass-by-reference because pointing to an outdated standard doesn't really help anyone.
Rant about pass-by-value vs pass-by-reference
Technically Java is pass-by-value
Reference: Is Java "pass-by-reference" or "pass-by-value"?
However, since that value often ends up being a pointer (Unless you are passing a primitive type) it might as well be pass by reference.
These days pass-by-reference languages are practically non-existent and more often than not telling someone a language is pass-by-value isn't very helpful to answering their question and may in fact give them the wrong idea when first learning how to code.
Reference: What (are there any) languages with only pass-by-reference?
Generally speaking, what most people want to know is where is my data and how is it passed. Since languages like Java and Python don't give you the option to take a pointer to data (No, reflection doesn't count), it raises the question of if I create a function that passes type Foo
, will it copy values within the entire Foo
object or just take a reference to the same foo object.
Take for example these two very similar programs in C and Java respectively.
struct {
int bar;
} Foo;
void editFoo(Foo foo) {
foo.bar = 85;
Foo a;
a.bar = 6;
editFoo(a);
printf("bar: %d", a.bar);
class Foo {
int bar;
}
static void editFoo(Foo foo) {
foo.bar = 85;
}
Foo a = new Foo();
a.bar = 6;
editFoo(a);
System.out.println("bar: " + a.bar);
The C version prints 6
, indicating that the function did not modify a
. To make it mutate the data within a
we would need to modify the function to specify that it takes a reference. However on the other hand, the Java version prints 85
indicating that it did pass a reference without any extra work on our part. In fact, Java doesn't give the option to do pass-by-value in the same way C does. If I want to pass a completely separate, but identical copy of Foo
, I would need to create a deepCopy
function and call it each time I passed the object. In my opinion, this is what should differentiate pass-by-value and pass-by-reference.
So answer your question...
You can just add it to the constructor or pass it through a function call.
public class Numbers {
public static void modifyList(ArrayList<ArrayList<String>> list) {
// You can access it here too!
list.get(0).add("two");
}
}
// Somewhere in main function
Numbers.modifyList(stats);
System.out.println(list.get(0).get(0));
// Should output "two"
On that note though, you may run into issues with your current code since stats
isn't static
but is being used in a static
context.