• Post Reply Bookmark Topic Watch Topic
  • New Topic

variable access from a constructor  RSS feed

 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got command line argument for a user name. I then initialized it in constructor of the class.

i created a thread from this constructor and tried using this name variable in run method.

It returns null . How do i use this variable inside a run method.

i tried using getName method which return name. But it returns null too.









 
Michael Krimgen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It would be useful if you posted the whole code.

You are saying that you initialize the name variable in the constructor of the AppServer class and start a thread from it.
However, in the code you posted you start a thread from the constructor of the AppClient class....

Cheers,
Michael
 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Krimgen wrote:Hi,

It would be useful if you posted the whole code.

You are saying that you initialize the name variable in the constructor of the AppServer class and start a thread from it.
However, in the code you posted you start a thread from the constructor of the AppClient class....

Cheers,
Michael



 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akila sekaran wrote:
Michael Krimgen wrote:Hi,

It would be useful if you posted the whole code.

You are saying that you initialize the name variable in the constructor of the AppServer class and start a thread from it.
However, in the code you posted you start a thread from the constructor of the AppClient class....

Cheers,
Michael





this is the code . Sorry that was a typo in the class name of my previous code sample
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First - you comment the line which is supposed to do the assignment.

Second - your parameter has the same name as the instance variable, and so the local parameter would hide the instance variable: so doing name = name is a non-action (assigning a value to itself). You want to qualify which name variable you want to assign to using the this keyword: this.name = name.
 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:First - you comment the line which is supposed to do the assignment.

Second - your parameter has the same name as the instance variable, and so the local parameter would hide the instance variable: so doing name = name is a non-action (assigning a value to itself). You want to qualify which name variable you want to assign to using the this keyword: this.name = name.



ohh yeahhh thanks a lot. "this" worked!! My code works fine now
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are tricks to make this less likely to happen in the future. One such trick is to always make your method parameters final: public AppServer(final int port, final String name). Then if you tried name = name you would get a compile-time error that points to the offending line rather than a run-time behavior problem which occurs in a different method at some later point.

Behaviorally, another thing I like to do is get in the habit of always using the this keyword to qualify instance variable references.
 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:There are tricks to make this less likely to happen in the future. One such trick is to always make your method parameters final: public AppServer(final int port, final String name). Then if you tried name = name you would get a compile-time error that points to the offending line rather than a run-time behavior problem which occurs in a different method at some later point.

Behaviorally, another thing I like to do is get in the habit of always using the this keyword to qualify instance variable references.[/quote


Oh okay got it! thank you steve!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!