When should you use a class method (static method)? When the instance of the method used?

A new colleague, write Java methods are static throughout modification, but only because it does not require new objects can be called directly. A class method abstracted, nothing wrong with a static modification, but in general, abstracted public methods, most of them should be placed util tools, rather than directly write in the class. static modification of the static method, although simple calls but also fast, but the actual programming, is not recommended.

This is not because the static methods take up more memory, but because we only understand the principle, in order to better apply to the actual programming. For example, why sometimes you need to use a singleton rather than a static method? For example some configuration and loading properties, the need exists throughout the life cycle, but only need to maintain one, but now these configurations and properties is obtained by the object-oriented approach, this time is necessary to use a single embodiment mode, although the static methods can solve, but the best embodiment mode or single mode.

So, when to use static methods?

1) in the conventional method tools it does not depend on specific examples, but the use of high frequencies.

"Light" Method 2) does not require objects because static methods can not directly use any non-static member, unless the instance as a parameter.

So, when should instantiate method to use it?
The method depends on the class object when you should use an instance method, because the change operations on a static method of static variables in multi-threaded scenario could easily lead to thread safety problems, this time, you must use instance method.

public class TestStatic {

	static class test {
		//静态成员变量
		static String a = "H";

		//修改该静态成员变量
		static void modify(String v) {
			a = v;
		}
	}

	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			new Thread(() -> {
				test.modify(Thread.currentThread().getName());
				System.out.println(Thread.currentThread().getName() + ":" + test.a);
			}).start();
		}
	}

}

Why is there the above result?

Java memory model, exemplary heap object stored, static member variables, array elements, etc., that each object has a corresponding lock exemplary monitor, while static class member variables .class all classified, so the above static method , equivalent to directly point to a shared variable v of reference, in the case of multiple threads, if one thread modifies a class static variable, and then acquires the eligible class variables, it is possible to get to the other is after modifying variable thread, the thread so that this situation of insecurity.

So how to solve this problem? The answer is locked.

public class TestStatic {

	static class test {
		// 静态成员变量
		static String a = "H";

		// 修改该静态成员变量
		static void modify(String v) {
			a = v;
		}
	}

	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			new Thread(() -> {
				synchronized (test.class) {
					test.modify(Thread.currentThread().getName());
					System.out.println(Thread.currentThread().getName() + ":" + test.a);
				}
			}).start();
		}
	}

}


输出:
Thread-4:Thread-4
Thread-7:Thread-7
Thread-6:Thread-6
Thread-5:Thread-5
Thread-1:Thread-1
Thread-2:Thread-2
Thread-0:Thread-0
Thread-3:Thread-3
Thread-9:Thread-9
Thread-8:Thread-8

Another way is to not use static methods, instance methods change, such as:

public class TestStatic {

	public static void main(String[] args) {
		test te = new test();
		for (int i = 0; i < 10; i++) {
			new Thread(() -> {
				te.modify(Thread.currentThread().getName());
				System.out.println(Thread.currentThread().getName() + ":" + te.a);

			}).start();
		}
	}

}

class test {
	// 静态成员变量
	String a = "H";

	public String getA() {
		return a;
	}

	public void setA(String a) {
		this.a = a;
	}

	public test(String a) {
		super();
		this.a = a;
	}

	public test() {

	}

	// 修改变量
	void modify(String v) {
		a = v;
	}
}

 

Published 224 original articles · won praise 34 · views 70000 +

Guess you like

Origin blog.csdn.net/weixin_39309402/article/details/105060026