static method vs new class

In my program I have a class "Team" that holds data for each object of team. Now I am trying to create an ArrayList of "Team" objects, but I was wondering if it is better practice to create a new class to do so, or if I should just perform that task in a static method of "Team".

I have searched for the answer, but only found comparisons of static vs non-static questions/answers. Where I am more interested in when I should make a new class vs using a static method.

Thank you for your help

Edit: Primarily what I am asking here is whether the methods "dataExtract" and "hasTeam" (as shown in the code below) should be in their own class, or kept as static methods where they are.

import java.util.ArrayList;

public class Team {
String country;
String skaterNames;
int points;

    public Team(String cntr, String sktrNames, int point){
        country = cntr;
        skaterNames = sktrNames;
        points = point;
    }

    public Team(){}

    public static ArrayList<Team> dataExtract(FileInput file){
        ArrayList<Team> al = new ArrayList<Team>();
        while (file.hasMoreData()){
            String tempLine = file.readLine();
            if (hasTeam(al, tempLine.substring(0, 3)) == -1){
                al.add(new Team(tempLine.substring(0, 3), tempLine.substring(3, 30).trim(), Integer.parseInt(tempLine.substring(37))));
            }
            else {
                al.get(hasTeam(al, tempLine.substring(0, 3))).skaterNames += (", "+ tempLine.substring(3, 30).trim()); 
                al.get(hasTeam(al, tempLine.substring(0, 3))).points += Integer.parseInt(tempLine.substring(37));
            }
        }
        return al;
    }
    public static int hasTeam(ArrayList<Team> al, String team){
        for (int i =0; i<al.size(); i++){
            if (((Team) al.get(i)).country.equalsIgnoreCase(team))
            return i;

        }
        return -1;
    }
}

Answers


In this particular case a static method is probably ok. The advantage of a non-static would be either future edits (i.e. if the class grows anything that changes the non-constant fields or references them would become an error source) and generally neatness. Keeping your methods inside variables allows you a living changing variable that can be very useful. In your particular case, though, that isn't the purpose of the class.

Static makes the method callable without creating an object of the class. Sometimes this can be good, for example all the Math class methods (Math.round, Math.ceil, etc.). Static should only be used when the method only uses it's own parameters. Even then sometimes it's better to leave it non-static. It's kept cleaner and you always know what you are actually editing.

If static methods use variables that are initialized in the constructor there could be a problem. If an object of this class isn't initialized, then the constructors aren't initialized. This could cause bad side effects and program errors.

Generally only use static if the class is merely a housing for a method that is completely standalone.


You should create non-static classes for re-usability, the ability to make subclasses, etc. If you have something like a "team" (whatever that may refer to), it's generally considered better practice to make a Team class and create several instances of it. Given that you're creating multiple Team objects, using static methods/field would just be too confusing and unorganized.


Need Your Help

Passing string via web service call using ksoap generating warnings

android web-services ksoap2 android-ksoap2

I have implemented an Android code that calls a SOAP web service. The web service takes a String parameter in the form "abc#bcd#efg#". When I pass such a String I get warnings as follows:

Updating controls in 2nd Window

xcode cocoa-bindings

I have two windows, 1 mainMenu and 1 window added by me

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.