this is a coding question for Java language.
May I know what is the difference between Option A and B?
Thread T1;
Option A: t1.currentThread();
Option B: t1 = Thread.currentThread();
The compiler gives an error for Option A.
this is a coding question for Java language.
May I know what is the difference between Option A and B?
Thread T1;
Option A: t1.currentThread();
Option B: t1 = Thread.currentThread();
The compiler gives an error for Option A.
May I know what is the difference between Option A and B?
One thing different ways.
The compiler gives an error for Option A.
Compiler shows error Variable 'threa' might not have been initialize
. because you did not initialize Thread t1
. Try below code for better understanding.
Thread threa = new Thread(new Runnable() {
@Override
public void run() {
// your thread work
}
});
threa.start();
threa.currentThread();
Now you can see in Thread Documentation. currentThread
method in Thread
class is static
.
Returns a reference to the currently executing thread object.
If you call Thread.currentThread()
that is right way to call static method. If you call t1.currentThread();
, then it is redundant to call static method from an instance/object.
So above code is redundant, you can call it via Thread class
.
Thread.currentThread();