3
\$\begingroup\$

I create class level static ArrayList by the following line.

static ArrayList<Student> studentList = null;

Then I create and fill ArrayList with some objects inside a function.

studentList = new ArrayList<Student>();
Write(student);

later I want to clear all of my ArrayList elements

studentList = new ArrayList<Student>();

Will my ArrayList removed and create a variable call studentList again by the above line??

or is it a good practice to clear like below??

studentList.clear();

or both

studentList.clear();
studentList = new ArrayList<Student>();
\$\endgroup\$

3 Answers 3

7
\$\begingroup\$

I hope that the function in which you actually created the object is also static otherwise the amount of mess that you can create has no limits. Make static variables only if it means sense that every instance of class should be able to use it. Don't use it for the sake of ease of sharing data like a global variable.

Also like Simon said you didn't create the ArrayList at the first line. You made a reference variable that is capable of holding a reference to that kind of object.

About the line studentList = new ArrayList<Student>(); - The object reference is gone from the current reference variable but whether or not the object reference is completely gone depends on whether you passed it around or not.

Use clear() instead of making a new object. But be aware that if you use clear() then there can be side-effects if you have passed the object reference around to some other object which is storing it. If you need to pass it around and want the ArrayList to be like a new one then it would be better to use new ArrayList<Student>(). That can help you to avoid side-effects of clearing. So choice is yours. Depends on the situation.

One more thing, if you are just using the list as a global variable only so that you don't have to create a lot of local variables then don't do that. Instead make a lot of local variables which fall out of scope as soon as the function ends. It may seem inefficient to create a lot of objects but if all the objects are short-lived then it will be garbage collected easily as GC is optimized for it. Also that can help avoid side-effects which can become a headache and break single-responsibility principle.

\$\endgroup\$
8
  • 3
    \$\begingroup\$ "Make static variables only if [...] object of class should be able to use it"... umm quite opposite static means it can be (should be) used without any object but just by the class itself. \$\endgroup\$ Commented Nov 23, 2013 at 18:10
  • \$\begingroup\$ yah yah I got ur points. yah I use static methods to access static members. Thanks a lot :) \$\endgroup\$
    – Eclayaz
    Commented Nov 23, 2013 at 18:49
  • \$\begingroup\$ @tintinmj My mistake, I meant every instance of class not the objects contained in the class. Static variables can be accessed without any instance of the class but can also be accessed by all instances of the class. I have not heard about any requirement about static variables should be used by the class only. public final static class variables are used by other classes a lot. \$\endgroup\$ Commented Nov 23, 2013 at 19:27
  • 2
    \$\begingroup\$ That's why I said can be but it leads to many confusion. This is a broad discussion and can't be fitted into a single comment. Search for some information and read this \$\endgroup\$ Commented Nov 23, 2013 at 19:44
  • \$\begingroup\$ @tintinmj I know what static variables are. Created once at class loading and should be accessed by class name and all about belonging to class(conceptually) rather than instances(which java allows). I have read oracle's Java SE tutorial on this. I am not getting what you are trying to say. What confusion are you referring to? \$\endgroup\$ Commented Nov 23, 2013 at 19:51
5
\$\begingroup\$

First of all, speaking of good practice, I would not have the variable as static. Using static variables is like using Singletons. There can be only one. You might regret this strategy later. Instead, if possible, I would declare the List in another object and not set it to static.

Technically, you said "I create class level static ArrayList by the following line." but on that line you only declare the ArrayList, you create it by calling studentList = new ArrayList<Student>();

That being said, I would say that it is a better practice to call clear on your list instead of recreating it. If you clear it, then there is no need to recreate it (unless you have sent the object reference to other places and you don't want those objects to be the same anymore). By not recreating your list, it also allows you to set the variable to final, which is good practice.

Here is how I would define your variable:

private final List<Student> studentList = new ArrayList<Student>();

I hope this makes sense.

\$\endgroup\$
3
  • \$\begingroup\$ I think I should create variable as static because like 18 different functions are working with the same ArrayList, otherwise I have to pass the same array each n every time. It's the best way isn't it? \$\endgroup\$
    – Eclayaz
    Commented Nov 23, 2013 at 18:56
  • 1
    \$\begingroup\$ @Eclayaz I don't know what the cleanest/best way to do it here is without looking at the rest of your code. \$\endgroup\$ Commented Nov 23, 2013 at 20:25
  • 1
    \$\begingroup\$ @Eclayaz: If you require access to the list in different places then yes you either need to pass the list around or the object which owns the list. btw: Passing the list around only passes the reference and not the entire list. Without looking at your code I can pretty much guarantee you that making it static is simply a bad idea. \$\endgroup\$
    – ChrisWue
    Commented Nov 25, 2013 at 1:31
5
\$\begingroup\$

Clearing and replacing with a new object are different operations. The correct thing to do depends on the context of your code, which you haven't provided. (In my opinion, you haven't provided enough code for a code review. As it stands, this question is more suitable for Stack Overflow. I suggest that you add more code to this question by editing it.)

In the absence of specific information about your usage, the preferred choice would be to just clear the list with studentList.clear(), because it creates the least amount of garbage to be collected. However, that may or may not be the right thing to do, depending on whether anything else also holds a reference to studentList.

Echoing @Simon's advice, I would advise you to use static only if you have a strong justification. Static variables are essentially global variables (though slightly better because they may have private access, and they are in a class's namespace).

\$\endgroup\$
1
  • \$\begingroup\$ got it! i can simply clean the arrayList according to my case. \$\endgroup\$
    – Eclayaz
    Commented Nov 23, 2013 at 18:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.