2

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

jlordo
37.9k7 gold badges62 silver badges84 bronze badges
asked May 6, 2013 at 17:30
3
  • You are using a method. That is why. List replaces what ever you pass to your method at a given moment. To become local. Commented May 6, 2013 at 17:33
  • Hint: What would happen if I write stringful(z)? Commented May 6, 2013 at 17:34
  • Since y is a variable that is local, my guess is that z will be passed into list and java will consider it a local variable without us having to declare it like what we did with y Commented May 6, 2013 at 17:42

4 Answers 4

4

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.

answered May 6, 2013 at 17:32
Sign up to request clarification or add additional context in comments.

Comments

3

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.

answered May 6, 2013 at 17:32

Comments

2

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.

answered May 6, 2013 at 17:32

Comments

0

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);
 }
}
answered May 6, 2013 at 17:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.