Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Thread.currentThread().getName() and getName()?

What's the difference between the static Thread.currentThread().getName() and getName() of a particular Thread instance?

like image 786
suisuisui Avatar asked Oct 31 '25 17:10

suisuisui


1 Answers

The difference is getName() is an instance method, meaning it operates on an instance of the Thread class.

Thread.getCurrentThread() is a class or static method, meaning it does not operate on an instance of Thread but rather on its class.

The ultimate difference is this: if you call Thread.currentThread().getName(), currentThread() will return an instance of Thread, which you can then call getName() on that instance. You cannot call Thread.getName() because getName() has to be called on an instance of Thread.

like image 61
Chris Blades Avatar answered Nov 02 '25 08:11

Chris Blades