• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting string

 
arjan pronk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

I've ran into a point where I can't seem to get it work correctly and was hoping you guys could point me in the right direction.

I want to get the String delay from the run() function. I've declared it in the checklocation class earlier but then it won't get changed.




[ June 01, 2008: Message edited by: arjan pronk ]
[ June 01, 2008: Message edited by: arjan pronk ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More of a beginner's question.

You are passing an argument to the badly-named ld method and it is local to that method and is not in scope when you reach the other method.
You are then declaring a String with the same name in the run method, which shadows the field, so no changes are applied to the field. If you want to apply those changes to the field, remove the declaration of "delay" from the run() method.
 
arjan pronk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was how my code was first





this works fine in this class but the problem is I need to get past another class,



[ June 01, 2008: Message edited by: arjan pronk ]
[ June 01, 2008: Message edited by: arjan pronk ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with the ld method is that you insist on returning its parameter. Suggest deleting the return statement, and changing the return type to void.
 
arjan pronk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if I don't return the String delay then I won't be able to use the string in that 2nd class.

think of it like this


outer
----
inner
----
function
----
delay
----
----
---- So I need to get it up 2 classes
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by arjan pronk:
But if I don't return the String delay then I won't be able to use the string in that 2nd class.
No such thing as a function in Java, only methods.

All you achieve with returning a String from the present form of the delay(String) method is sending the String back where it came from. You are not assigning a field, nor are you sending the String anywhere else. I still think you ought to have a void return type for that method. It might be more appopriate to ahve a setDestination() method and keep the String as a field. BTW: Delay doesn't seem to me to be a good name from the String.
 
arjan pronk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the end, I had to make delay static for it to work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic