Imonar Smith
Imonar Smith

Reputation: 389

Java - ArrayList and creating an object

The header question is a little bit tricky, so here's my problem:

1- I created an object y of an arraylist and I assigned it as a reference variable, so far so good.

ArrayList<String> y = new ArrayList<String>();

2- Here I added an element to the array:

y.add("Hello"); y.add("GoodBye");

Now here's the part that I don't understand, now when I create a method that returns a String:

public String stringful(ArrayList<String> list)

Now when I try to get the size() of the array, I use list.size() but why? Isn't y the original object and I should use y.size() although it doesn't work so that's why I'm here. Thanks

Upvotes: 2

Views: 4640

Answers (4)

John Kane
John Kane

Reputation: 4453

Take a look at this tutorial. It goes through scoping and seems to be fairly clear. For a quick example:

public class Test{
    private List<String> globalY;//accessable to anything in the class

    public Test(){
        globalY=new ArrayList<String>();
        globalY.add("global");   
    }

    //here, methodParam is only accessible inside this method
    public void example(ArrayList<String> method){
        //this list is only available inside this method
        List<String> local=new ArrayList<String>();
        local.add("local");

        System.out.println(globalY(0));
        System.out.println(method(0));
        System.out.println(local.get(0));
    }

    public static void main(String[] args){
        Test test=new Test();
        List<String> localY=new ArrayList<String>();
        localY.add("method");

        test.example(localY);
    }
}

Upvotes: 0

christopher
christopher

Reputation: 27356

With this method header:

public String stringful(ArrayList<String> list)

You're declaring that this method will use an object, of type ArrayList<String> called list. Because you declare y locally, the other method has no idea that even exists. Reference to y from that method won't compile, assuming of course that it is a local variable.

Pointers in Java

When you create a new object, you use a pointer to that object. For example, when you declare:

ArrayList<String> y;

you're creating a pointer. When you add the code:

ArrayList<String> y = new ArrayList<String>();

This puts the pointer to a new ArrayList<String> object into the y variable. So when you pass y as a parameter into a method, you're passing a pointer to the object that y also points to, not the object iself. This is why your code works with list.size(). All it's doing is getting the size value of the exact same object, just with a different pointer.

Summary

In Summary, it actually is the same object. It's just different pointers, looking at that object.

Upvotes: 4

SLaks
SLaks

Reputation: 888203

y is a local variable.
It only exists inside the function you declared it in.
y refers to an ArrayList instance.

When you write stringful(y), you pass this ArrayList instance as a parameter to the function.
Within the function, you can refer to the value of the parameter using the list "variable" (it's actually a parameter, but it looks like a variable), which refers to the same instance you passed when you called it.

Upvotes: 2

user2282175
user2282175

Reputation:

You are passing the reference to the ArrayList<String> y to method stringful to list reference variable and hence will use list.size(). y is unknown to that method unless y is an instance or class variable which is visible in that method. list is the local variable here which is used to pass the reference of y. This way you can pass a reference of any ArrayList<String> to that method and calculate its length. If you had used y.size() in that method (provided y is visible in that method), then you could have been able to calculate only the length of ArrayList<String> referenced by y.

Upvotes: 3

Related Questions