• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

variable access from a constructor

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.









 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic