Hi,
Just started learning Threads for my certification but my curiosity drove me beyond certificaton objectives.
I tried to find the
Thread class source code but could not fully understand.
in my PSVM(
test class - public static class void main) method, I am coding the below code (where class B extends Thead)
B b = new B("b");
Compiler forces me to use the below constructor
B(
String s) {} //It does not force me to pass s to the super constructor
My question is
super() statement is added by default, when we inherit. I also know there is a no-arg constructo in Thread class, so compiler wont complain. So, all Ok, untill now.
but how does b.getName() returns "b" ?
Constructor of B don't even pass "b" to super class, so super class dont know anything about the name "b", so it returns "Thread-0" in my JVM implementation.
However, If I add super(s) statement inside B's constructor, I get the name displayed as b.
I am new to
java, but I am missing some fundamentals here. Please help me.
b.getName() for sure refers to the method in Thread class (as B extends Thread, and class B has not overridden getName() method). at somepoint later, If I execute the statment, b.getName(), I get "b". I actually expect "Thread-0". I am wondering, how did the name instance b is aware of his name "b". (in otherwords how was the name passed up in the hierarchy in constructors).
This is interesting topic to me, but I might not be clear. So, to be clear, I paste my playing code here, so you get a picture of me. I have lot of commented code, so please bear with me