简体   繁体   中英

Should i make this method static?

I am writing an android application that parses an xml feed about car parks. I've created a separate class that receives an xml feed as a string, this class will then parse the string, creating objects for each car park found in the feed. I will use this each time I refresh the xml feed and need to parse it, should I make this class static or not. I am still not 100% sure on when to make something static or not.

My code:

public class XMLParser2 {

private static CarPark carPark;
private static ArrayList<CarPark> carParkListings;

// Parse XML string and save each car park object created within an Arraylist collection
public static ArrayList<CarPark> parseXML(String xml) throws XmlPullParserException, IOException {

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();
    xpp.setInput(new StringReader(xml));
    int eventType = xpp.getEventType();
    carParkListings = new ArrayList<CarPark>();

    while (eventType != XmlPullParser.END_DOCUMENT) {

        if (eventType == XmlPullParser.START_TAG) {
            if (xpp.getName().equalsIgnoreCase("situation")) {
                // Create new CarPark object to hold car park data
                carPark = new CarPark();
            }
            // Check start tag for each element wanting to obtain and save in the 
            // carPark object using its associated set method
            if (xpp.getName().equalsIgnoreCase("carparkstatus")) {
                String status = xpp.nextText();
                if (status.matches("enoughSpacesAvailable"))
                    carPark.setCarParkActive("Open");
                else if (status.matches("carParkClosed"))
                    carPark.setCarParkActive("Closed");
                else
                    carPark.setCarParkActive("Full");
            }
            if (xpp.getName().equalsIgnoreCase("overallstarttime")) {
                carPark.setDateCreated(xpp.nextText());
            }
            if (xpp.getName().equalsIgnoreCase("latitude")) {
                Double latitude = Double.parseDouble(xpp.nextText());
                carPark.setLatitude(latitude);
            }
            if (xpp.getName().equalsIgnoreCase("longitude")) {
                Double longitude = Double.parseDouble(xpp.nextText());
                carPark.setLongtitude(longitude);
            }
            if (xpp.getName().equalsIgnoreCase("carparkidentity")) {
                String[] identity = xpp.nextText().split("\\:");
                carPark.setCarParkName(identity[0]);
                carPark.setCarParkID(identity[1]);
            }
            if (xpp.getName().equalsIgnoreCase("occupiedspaces")) {
                int occupiedSpaces = Integer.parseInt(xpp.nextText());
                carPark.setOccupiedSpaces(occupiedSpaces);
            }
            if (xpp.getName().equalsIgnoreCase("totalcapacity")) {
                int totalCapacity = Integer.parseInt(xpp.nextText());
                carPark.setTotalSpaces(totalCapacity);
            }
        }
        // Check if the tag is an end tag
        else if (eventType == XmlPullParser.END_TAG) {
            if (xpp.getName().equalsIgnoreCase("situation")) {
                // Calculate total spaces free
                carPark.setFreeSpaces();
                // Add CarPark class object to carParkListings ArrayList collection
                carParkListings.add(carPark);
            }
        }
        // Move to next event
        eventType = xpp.next();
    }
    // Return ArrayList collection
    return carParkListings;
}

}

Better make it non static. You manipulate objects inside the class and if multiple classes call the same method in the same time you might have problems. For example one class calls this method and it start executing and in the same time another class calls the method and reaches the line carPark = new CarPark(); before the first one has finished. It will compromise the results.

Better use instances and make the fields carPark and carParkListings non static.

If you don't want to create any instance of XMLParser2 class when you call the parseXML method, you can make it static.

But what you should not do is use the global static property in your parseXML method.

private static CarPark carPark;
private static ArrayList<CarPark> carParkListings;

Those two variables need to define local inside the method only.

If you do not maintain any other states in XMLParser2, I think it is a good candidate for a static method since it looks like an utility method to me, but you should make the carPark and carParkListings local variables of the method itself. One should use a static method if it does not use any instance variables. You only need to specify input as parameter to the method and expect to get back some result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM