A super() is used to execute super class's constructor from within sub class's constructor. In this program, the output remains the same with or without the super() inside the constructor. So could you please explain the difference between having and not having super() in this scenario?
class SomeThread extends Thread {
String ThreadName;
SomeThread(String ThreadName) {
super(ThreadName); //Why Oh Why ?
this.ThreadName = ThreadName;
}
public void run(){
for(int ctr=1; ctr<=10; ctr++) {
System.out.println("From "+ThreadName+"...."+ctr);
try {
sleep(1000);
}catch(Exception e) {
System.out.println("Exception in "+ThreadName);
}
}
}
}
class ThreadAliveDemo {
public static void main(String[] args) throws Exception {
SomeThread FThread = new SomeThread("Some Child Thread");
System.out.println("Some Child Thread is alive "+FThread.isAlive());
FThread.start();
System.out.println("Some Child Thread is running.."+FThread.isAlive());
for(int ctr = 1; ctr<=5; ctr++) {
System.out.println("From.."+Thread.currentThread().getName()+"..."+ctr);
Thread.sleep(1000);
}
FThread.join();
System.out.println("Some Child Thread is alive.."+FThread.isAlive());
for(int ctr=6; ctr<=10; ctr++) {
System.out.println("From.."+Thread.currentThread().getName()+"..."+ctr);
Thread.sleep(1000);
}
}
}
Why? The Sun compiler says, call to super must be first statement in constructor . The Eclipse compiler says, Constructor call must be the first statement in a constructor . So, it is not stopping you from executing logic before the call to super() .
super can be used to refer immediate parent class instance variable. super can be used to invoke immediate parent class method. super() can be used to invoke immediate parent class constructor.
The super(String) statement is an explicit super class constructor call. All java constructors must call one or another constructor from their super classes. By default the no-args constructor is invoked, but in this way you can explicitly specify which super class constructor to invoke.
Java static code analysis: Classes extending java. lang. Thread should override the "run" method.
Calling the superclass constructor Thread(String name)
you explicitly set the system thread name which you can later get with Thread.currentThread().getName()
. If you don't call the superclass constructor explicitly, a default constructor will be used which will assign the default thread name like Thread-1
. Those thread names can appear in debugger UI, jstack
output and so on, so it's a good idea to assign meaningful names to your threads. For example, when running your current code in Eclipse debugger you'll see:
So you can see Thread [Some Child Thread]
. But if you remove that super call, you'll see the following:
Now it's just Thread [Thread-0]
.
If you do make the call to super as you have done, then you will end up calling the following constructor according to the JavaDoc:
public Thread(String name)
Allocates a new Thread object. This constructor has the same effect as Thread (null, null, name).
So super(ThreadName)
calls super(null, null, ThreadName)
.
If you remove your call to super(ThreadName)
, then the JVM will call the empty constructor super()
automatically for you. According to the JavaDoc you will end up making an almost identical call:
Thread public Thread() Allocates a new Thread object. This constructor has the same effect as
Thread (null, null, gname), where gname is a newly generated name. Automatically generated names are of the form "Thread-"+n, where n is an integer.
So super()
calls super(null, null, gname)
, where gname
is an auto-generated name for the Thread.
The only difference in behavior between calling and not calling super() is the assignment of a Thread name.
The super(ThreadName);
statement invokes the Thread(String name)
constructor of Thread class. As the argument suggests, it sets the name of the thread
.
While setting thread
's name may not be directly useful in this example, in general, thread
name can be useful when debugging, since you can add thread names to your application's logs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With