aspose file tools
The moose likes Beginning Java and the fly likes variable access from a constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "variable access from a constructor" Watch "variable access from a constructor" New topic
Author

variable access from a constructor

akila sekaran
Ranch Hand

Joined: Jun 12, 2012
Posts: 48
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

Joined: Jul 08, 2012
Posts: 34
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

Joined: Jun 12, 2012
Posts: 48
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

Joined: Jun 12, 2012
Posts: 48
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

Joined: Jan 28, 2003
Posts: 3041
    
    4

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.


Steve
akila sekaran
Ranch Hand

Joined: Jun 12, 2012
Posts: 48
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

Joined: Jan 28, 2003
Posts: 3041
    
    4

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

Joined: Jun 12, 2012
Posts: 48
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!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: variable access from a constructor
 
Similar Threads
Break the loop using threads
Question with interrupt ()
Gloal Variables in Java
Runtime.exec() file problem
Global Variables in Java