Reputation:
I hope this illustration will make my question clear:
class someThread extends Thread{
private int num;
public Testing tobj = new Testing(num); //How can I pass the value from the constructor here?
public someThread(int num){
this.num=num;
}
void someMethod(){
someThread st = new someThread(num);
st.tobj.print(); //so that I can do this
}
}
Upvotes: 2
Views: 130
Reputation: 1499860
For one thing, having a public field is a bad idea to start with IMO. (Your names aren't ideal either...)
All you need to do is initialize it in the constructor instead of inline:
private int num;
private final Testing tobj;
public someThread(int num) {
this.num = num;
tobj = new Testing(num);
}
(You don't have to make it final - I just prefer to make variables final when I can...)
Of course, if you don't need num
for anything else, you don't need it as a field at all:
private final Testing tobj;
public someThread(int num) {
tobj = new Testing(num);
}
Upvotes: 6
Reputation: 46398
Why not just initialize your object in the constructor ??
public Testing tobj ; //How can I pass the value from the constructor here?
public someThread(int num){
this.num=num;
tobj = new Testing(this.num);
}
Upvotes: 1