Found an interesting question today
Can not be used directly! The java method name or variable name position cannot receive parameters.
Give an example:
It's actually quite understandable.
Because in essence String str declares a reference address on the stack, and this address points to the specific object generated in the heap. So you have to be sure at the beginning, you can't pass it through parameters. The point of reference should be unambiguous. ( please point out if it is wrong )
In fact, this function can be achieved in other ways.
You just want to manipulate this object later, so you want to give each object a name.
Arrays can be passed.
This can be referred to later through the array subscript.
Why am I watching this. . . . Because I want to generate a for loop to generate multiple threads, and give each thread a name . just thought of here
It is very simple in the thread. The implementation of this function is new Thread(Runnable target , String name)
public class TestThread2 {
public static void main(String[] args) {
TestThread2 t = new TestThread2();
for(int i=0;i<10;i++) {
new Thread(t.new MyThread(), i+"线程").start();
}
}
class MyThread extends Thread {
public void run() {
System.out.println("你好"+ Thread.currentThread().getName());
}
}
}
result
refer to! https://wenda.so.com/q/1376719850069864