0

We have to create a object of any class to use their funtionalities unless those are static functionalities. But why we dont need to create a ArrayList object to use its methods like add, contains etc..

ArrayList<Egg> myList = new ArrayList<Egg>();
myList.add(a);

According to my understanding, myList is just variable which holds ArrayList object's reference of type ArrayList class. So again how can we write following without passing object to myList.

ArrayList<Egg> myList;
myList.add(a);

Complete code:

import java.util.ArrayList;

public class DotCom {
    private ArrayList<String> locationCells;

    public void setLocationCells(ArrayList<String> loc)
    {
        locationCells = loc;
    }

    public String checkYourself(String userInput)
    {
        String result = "miss";
        int index = locationCells.indexOf(userInput);
        if (index >= 0) {
            locationCells.remove(index);
            if (locationCells.isEmpty()) {
                result = "kill";
            }
            else
            {
                result = "hit";
            }
        }
        return result;
    }

    //TODO:  all the following code was added and should have been included in the book
    private String name;
    public void setName(String string) {
        name = string;
    }
}

PS I am referring heads first java book.

7
  • You're not giving near enough code to let the question be answerable. How is myList being assigned a reference, for instance? Commented Jun 17, 2019 at 15:35
  • 1
    You do need to create an instance; that will not work. Commented Jun 17, 2019 at 15:36
  • 4
    And your 2nd code may compile, but will throw a NullPointerException if you try to run it Commented Jun 17, 2019 at 15:36
  • "So again how can we write following" - well you can write a lot of things and depending on the editor/ide you might not see any error. The compiler or at least the runtime would complain about that though. Commented Jun 17, 2019 at 15:38
  • But right now, all we can do is guess because you've not yet posted a complete small program, a minimal reproducible example, that illustrates the point you're trying to make. Commented Jun 17, 2019 at 15:40

1 Answer 1

1

The ArrayList reference is being set in the setter method:

public void setLocationCells(ArrayList<String> loc)
{
    locationCells = loc;
}

If this method is not called, and the reference not set before trying to use the ArrayList, then the code will throw a NullPointerException.

Side note: This does not look to be safe code, since it can be easily run incorrectly and so a NPE is easy to create. Better perhaps to set the ArrayList (List is even better) in a constructor.

Sign up to request clarification or add additional context in comments.

4 Comments

@Dexter: it's exactly what the method parameter says that it is, no?
Okay got it!! Thank you.
@Dexter: more importantly, in the future, please strive to ask more complete questions. If we don't have to extract the key information from you, the process goes much smoother. Please read the How to Ask for site best-practices when asking.
I will take my time when composing a question next time. Thank you for the suggestion.

Your Answer

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.