Java - what is better to choose, static or nonstatic methods

286 views Asked by At

So I want to create class where will be created few objects and later I will add them to Map. There will be three maps. Map of Objects1, map of Objects2 and map of relations between object2 and objects1. Like now I have something like this:

public class SomeClass {
    private static final Map<Integer, Object1> OBJECTS1 = new HashMap<>();
    private static final Map<Integer, Object2> OBJECTS2 = new HashMap<>();
    private static final Map<Integer, List<Integer>> OBJECTS2_IN_OBJECTS1 = new HashMap<>();

    static {
        Object1 object1 = new Object1(1, "someStringForObject1");
        Object2 object2 = new Object2(1, "someStringForObject2");

        object1.setObject2(object2);
        OBJECTS1.put(object1.getId(), object1);
        OBJECTS2.put(object2.getId(), object2);

        SomeClass.addObject2ToObject1(object2.getId(), object1.getId());
    }

    public static Collection<Object1> getObjects1() {
        return OBJECTS1.values();
    }

    public static Object1 getObject1(int id) {
        return OBJECTS1.get(id);
    }

    public static void addObject1(Object1 object1) {
        OBJECTS1.put(object1.getId(), object1);
    }

    public static Collection<Object2> getObjects2() {
        return OBJECTS2.values();
    }

    public static Object2 getObject2(int id) {
        return OBJECTS2.get(id);
    }

    public static void addObject2(Object2 object2) {
        OBJECTS2.put(object2.getId(), object2);
    }

    public static void addObject2ToObject1(int object2Id, int object1Id) {
        List<Integer> objects1OfObject2 = OBJECTS2_IN_OBJECTS1.get(object2Id);
        if (objects1OfObject2 != null) {
            objects1OfObject2.add(object1Id);
            OBJECTS2_IN_OBJECTS1.put(object2Id, objects1OfObject2);
        } else {
            objects1OfObject2 = new ArrayList<>();
            objects1OfObject2.add(object1Id);
            OBJECTS2_IN_OBJECTS1.put(object2Id, objects1OfObject2);
        }
    }
}

And later in other class I want for example add objects to map etc. Just make some operations on these maps. For example:

class SomeOtherClass{
    public Collection getObjects() {
        return SomeClass.getObjects1();
    }

    public Object1 addObject1(int idOfObject1, String someString, int idOfObject2) {
        if (SomeClass.getObject1(idOfObject1) != null) {
            System.out.prinltn("Object with this id already exists.");
            return null;
        }

        Object1 object1 = new Object1(idOfObject1, someString);
        SomeClass.addObject2ToObject1(idOfObject2, idOfObject1);

        SomeClass.addObject1(object1);
        return object1;
    }
}

So here is my question. Methods in "SomeClass" should be static or nonstatic, so I will be creating "SomeClass" object in every method:

class SomeOtherClass{
    public Collection getObjects() {
        SomeClass someClass = new SomeClass();
        return someClass.getObjects1();
    }

    public Object1 addObject1(int idOfObject1, String someString, int idOfObject2) {
    SomeClass someClass = new SomeClass();
        if (someClass.getObject1(idOfObject1) != null) {
            System.out.prinltn("Object with this id already exists.");
            return null;
        }

        Object1 object1 = new Object1(idOfObject1, someString);
        someClass.addObject2ToObject1(idOfObject2, idOfObject1);

        someClass.addObject1(object1);
        return object1;
    }
}
1

There are 1 answers

0
GhostCat On BEST ANSWER

The generic rule is: static is an abnormality in OO design. It should be the exception. It leads to extremely tight coupling between your classes; it doesn't play nice; neither with polymorphism nor with unit-testing.

Thus: you only use static when there are very good reasons to do so.

Looking at your example code, I really don't see any need or the aforementioned "good reasons" to do anything using static. So my recommendation: step back; and simply do some reading. Like here or there (esp. the answer from Alfred).