-2

How will i pass my 2d arraylist to number class, so that when i change it in there, my arraylist in test will also change and vice versa. like both class sharing the same variable; i do pass by reference on C, but i dont know in java

import java.util.ArrayList;

public class test {

    ArrayList<ArrayList<String>> stats = new ArrayList<ArrayList<String>>();
    
    public static void main(String[] args)
    {
        stats.get(0).add("one");
        System.out.println(stats.get(0).get(0));
    }
}

I want this number class to be able to access and change the arraylist variable

import java.util.ArrayList;

public class numbers {
    //add value
    //change value
}
Jeric
  • 25
  • 5
  • Why not simply use getter and setter methods and include a test field within the numbers class? This is basic Java, and I encourage you to read an introductory tutorial or textbook on use of these. – Hovercraft Full Of Eels Aug 15 '20 at 00:19

1 Answers1

1

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.

Locke
  • 7,626
  • 2
  • 21
  • 41
  • 1
    *"java will always pass by reference"* -- Java is "pass-by-value" ***ALWAYS***. Please read: [Is Java “pass-by-reference” or “pass-by-value”?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Hovercraft Full Of Eels Aug 15 '20 at 00:17
  • Yes that is true, but I think it is easier to approach when thinking of it as pass by reference as a beginner. Since almost all questions related to pass by value/reference are focused on determining if the data within a class is being coppied, I feel that calling it pass by value can feel a bit misleading. I'll edit my post clarify what I meant. – Locke Aug 15 '20 at 00:34