(1) static:: FooUtil.doFoo()
(2) non-static:: FooUtil.getInstance().doFoo()
Please correct me if I'm wrong:
In #1, as long as state (static variables) is not being updated then this should be thread safe
In #1, as long as state (static variables) is not updated like adding something to a set, then memory usage should be ok
#2 could become non-thread safe if it updates static variables
#2 could become a memory hog if it modifies state, like adding something to a set
If you're working on a team, what kind of tricks can you use to prevent others from introducing code that is not thread safe or poor memory usage? Is there a tool or technique you could use to prevent this? Besides code reviews, which may be difficult to keep up with.
Thank you
Answer about usage of Java static methods
Edit: I don't know much about threads (except in the broadest of terms).
see this, http://www.programcreek.com/2014/02/how-...
static methods are methods for class not object. The different between class and object is another question. static methods can be thread-safe see the above link for details. example, "count" is class varibale and "name" is object variable.
public class Program {
public static void main(String[] args) {
Cat mary = new Cat("mary");
Cat aimee = new Cat("aimee");
System.out.printf("%d %d %d%n", Cat.count(), mary.count(), aimee.count());
}
}
class Cat {
private static int count;
private String name;
public Cat(String name) {
this.name = name;
synchronized(this) {
count++;
}
}
public static int count() {
return count;
}
@Override
public String toString() {
return this.name;
}
}