1. What does Thread.currentThread()
do?
It returns a reference to the currently executing thread — the thread that is calling this method.
Thread t = Thread.currentThread(); System.out.println(t.getName());
If the current thread’s name is “main”, it will print “main”. It tells: “Who is running this code right now?“
2. Why does it have to be a static
method?
Because you don’t already have an instance of the current thread when you want to ask:
- You cannot call an instance method like
this.currentThread()
because you don’t have an instance yet. - If it was a non-static method, you would need to already know which thread object you are in — but that’s exactly the thing you are trying to find out.
Therefore, it must be static, because you are asking the Thread class itself:
In simple words, You need a way to access the current thread without already having a thread object.
3. What would happen if it was not static?
If currentThread()
were non-static:
- You would need an object to call it on.
- But you don’t have the object yet — you need to discover it.
- It would cause a paradox: “You need the current thread to get the current thread.”
So static solves this neatly — you don’t need an object; you ask the class directly.