wood burning stoves 2.0*
The moose likes Java in General and the fly likes How can I get a run() method to return a string variable, when run() method is required to be void ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can I get a run() method to return a string variable, when run() method is required to be void ?" Watch "How can I get a run() method to return a string variable, when run() method is required to be void ?" New topic
Author

How can I get a run() method to return a string variable, when run() method is required to be void ?

Mary Dawson
Ranch Hand

Joined: Feb 08, 2009
Posts: 36
Dear JavaRanch members,
I was wondering if you could help me with a conundrum I have encountered with my course work.
I am requested to make a Thread subclassed program output in its run method the 'for do'loop incrementations of int 'i' and also the initialisement of String Name, as given by the getName() method. The problem I encounter with this request is that if I use the syntax System.out.print(Name) ; then console returns :-
missing return statement
^
1 error

if I try invoking the method with System.out.print(getName()); then again console returns:-
missing return statement.
^
1 error

however if I proceed accordingly to write :- return Name, then console then returns:-

'cannot return a value from method whose result type is void : return Name
^
1 error

Is there a way around my getting these error returns? I have to get the run method to return the getName() method's output- but using a return statement seems to contradict the requirements of the run() method - which is to return a void type.
The code to which all this refers to is as follows:-


oF course, if I only include the first statment of the for loop, it'll output the incrementations of the value of i, but that is not what has been required of me.
Thank you in advance for any advice you can offer.
Yours
Mary Dawson

A man of words and not of deeds is like a garden full of weeds, and when the weeds begin to grow, its like a garden full of snow, and when the snow begins to fall- its like a bird upon the wall and when the bird begins to fly, its like an eagle in the sky
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Can you provide us the error message exactly as they appear? .... it is not the run() method that is missing the return statement.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Not at all.
Find out about the Callable interface. You can return anything from its method, which I think is call().
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2176
    
  47
Your getName method should be called setName as it's taking a parameter which it uses to set the object's Name field. It also should not be declared as returning a String value. You then need a getName() method which does return the Name field.
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
The Thread class already has getName and setName methods and also a private member variable called "name" so you dont need to redefine those. You should call setName once on the instance of Thread you created in the OneThreadApp main method, passing a string like "MyThreadName", and then inside the run() method of the thread call getName() method to get access to this name and print it. HTH


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Mary Dawson wrote:
I have to get the run method to return the getName() method's output- but using a return statement seems to contradict the requirements of the run() method - which is to return a void type.


This is impossible, you cant return a string from a void method. You can use the Callable interface as Campbell said but if its a simple assignment maybe they just mean print the name with the i value, as you were doing with System.out.println?
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

i just copied this code into eclipse, and had to remove the getname/setname method as it is final in thread so can't be overridden.
I included the main in the same class and then it ran exception free.

I think you need to go back to your teacher for clarrification.




output :
The value of i is:-0
Thread-0
The value of i is:-1
Thread-0
The value of i is:-2
Thread-0

for ever as there is no loop endpoint
Mary Dawson
Ranch Hand

Joined: Feb 08, 2009
Posts: 36
Dear Javaranch members,
Thank your for your respective responses to which I will reply to in the order in which
you responded.

Dear Henry Wong:

If I code the program this way:-

Then it runs thus:-

- Great, however I was told in given text that I ought to do it using the getName method-
something the console seems dead against, in one way or another, so am not sure if I am
'cheating' in using the Call() method.

Dear Tonay Docherty,
In the code above, I hope you have noticed that I have taken note and recoded accordingly.
The only oversight has been in keeping the data type return for the setName, however if
I lose it thus:-

Thank you for your help thus far.
Upon posting this,I have noticed several more replies, the gist of which suggest that a run method can't return anything but a void and as such its signature must be thus writ. My teacher considers that it can invoke a method to return that value - ie using System.out.println(getName()) in the run method, but the console doesn't agree - and has the last word.
Yours
Mary Dawson.





Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Upon posting this,I have noticed several more replies, the gist of which suggest that a run method can't return anything but a void and as such its signature must be thus writ. My teacher considers that it can invoke a method to return that value - ie using System.out.println(getName()) in the run method, but the console doesn't agree - and has the last word.



Hmmmm.... I think you have a serious issue with terminology here. And if your instructor agrees with you, then he/she has a serious issue with terminology too.

When you say "a method returns something", it means that the method returns a value to the calling method. The value is returned to the caller. A System.out.print() is *not* a return. It is printing something to system out (which by default, is the screen). This is why there is confusion here -- with this question, everyone here is assuming that you mean the Java definition of returning something from a method.

And there is no "gists" or "suggest", etc... We are being quite clear. A void method can't return a value. And that is by the Java definition, of what a void method can do.

Henry
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Mary, you seem to be missing a major part of inheritance, try using getName() in your run method.

firstly read the api for thread:
java api- thread

and notice in the method summary it already contains a getName and setName method, because you extend this class you can use it's public and protected methods.

do you know about overriding? if not go and find out about that either from your teacher or the books/ answers in this link:
javaranch answer

These methods are declared as Final methods that means the builder of the Thread class has declared "no one is allowed to override this method, this is the very final version" (this explains the error messages when you tried to create the setname and getname methods)

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Mary Dawson wrote:
Dear Henry Wong:

If I code the program this way:-

[EXAMPLE DELETED for brevity -- but see post above]

If I change the very last line of program OneThread to :-

[EXAMPLE DELETED -- again, see post above]

I get the same response.
If I change the last line to:-

[EXAMPLE DELETED -- again, see post above]

I get :-

[EXAMPLE DELETED -- again, see post above]


Basically, you tried to override a method that was declared as final -- meaning you are not allowed to override it. The compiler complains, telling you of the problem.

You tried to fix it by changing the last line. This is completely not related to the problem, so the compiler continues complaining, telling you of the original problem.

You tried to fix it again. Again, by doing something that is completely not related, but this time... in a way that breaks something else -- trying to return something from a void method. The compiler complains, telling you of both the original problem, and the new problem that you introduced.

And your conclusions of these events, was this...

ie: you get stuck in a no-win situation: Without a return statement in the run() method -
you get a console request for a return statement, if a return statement is put in - then
you are requesting a necessarily void run method return a value which must be of some data
type - which again console will reject.


Quite frankly, I don't even know how to address your issue here. I can't figure out how you came to this conclusion...

Henry
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2176
    
  47
the gist of which suggest that a run method can't return anything but a void and as such its signature must be thus writ. My teacher considers that it can invoke a method to return that value

You are talking about two different things here. The run() method as defined by the Runnable interface can not return a value. It is declared as havaing a void return type and this can not be changed.
The code in the run method can invoke (call) other methods that return values, but these values will be returned to the caller (ie the code in the run method) but they cannot be returned from the run method.

ie using System.out.println(getName()) in the run method,

The println() method doesn't return a value it sends the string returned from the call to getName() to the output stream to the console.
Mary Dawson
Ranch Hand

Joined: Feb 08, 2009
Posts: 36
Dear Wendy Gibbons,
I have copied your code (losing the Overide keyword- console didn't like

it)
thus:-

I guess that the Thread is returned as that is the class the program is

subclassed from (?), but upon trying to pass the instantiation
a name ie modifying program to:-
[CODE]
public class TwoThread extends Thread
{
int i;
private String Name;

public TwoThread(String Name)
{
this.Name = Name;
}

public String getName()
{
return Name;
}



public void run()
{
while ( true )
{
for ( i = 0; i < 3; i++ )
{
System.out.println( "The value of i is:-" + i );
System.out.println( getName() );
}
}
}

public static void main( String[] args )
{
TwoThread tt = new TwoThread("Tick tock");
tt.start();
}
}
[/CODE/]
It returns :-
[CODE]
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\user>cd C:\lab_files\Lesson_6

C:\lab_files\Lesson_6>set PATH=C:\jdk1.42/jdk\bin;%PATH%

C:\lab_files\Lesson_6>javac TwoThread.java
TwoThread.java:11: getName() in TwoThread cannot override getName() in

java.lang
.Thread; overridden method is final
public String getName()
^
1 error

C:\lab_files\Lesson_6>
[/CODE/]
I will have to stick to the call() method for now.

Dear Henry Wong,
For better or worse my instructor reckons

System.out.println(returnName());

ought to output a value, but my Console seems to reckon it will not work- and states that such a line of code contradicts the 'void' run method signature, as in

public void run()

Whether those more knowledgeable than I consider the value is being determined in a method outside the run() method or within it seems irrelevant if the console deems that the run () method can't and won't output that value.

You write:
"A void method can't return a value. And that is by the Java definition, of what a void method can do."
Do you not mean "what a void method can't do" (ie: return a value)?

Thank you for your attention.
Yours
Mary Dawson.


Mary Dawson
Ranch Hand

Joined: Feb 08, 2009
Posts: 36
Dear Wendy, & Henry & any other JavaRanch members reading,

I don't know how I came by these conclusions either, but hell y'
gotta make the best of a lousy situation - ja?

I take it that I get the error returns 'cause methods
setName() and getName() are already defined in the Thread class
which my program is subclassed from - thus to use them I
must define them exactly as they have been defined in
the Thread class, ie:-

[CODE]
public final String getName()
{
return Name;
}

public final String setName(String Name)
{
this.Name = Name;
}
[/CODE/]
but defining them thus in a similar program (which includes
the main) re:
[CODE]
public class TwoThread extends Thread
{
int i;
private String Name;

public TwoThread(String Name)
{
this.Name = Name;
}

public final String getName()
{
return Name;
}

public final String setName(String Name)
{
this.Name = Name;
}



public void run()
{
while ( true )
{
for ( i = 0; i < 3; i++ )
{
System.out.println( "The value of i is:-" + i );
System.out.println( getName() );
}
}
}

public static void main( String[] args )
{
TwoThread tt = new TwoThread("Tick tock");
tt.start();
}
}
[/CODE/]
gives:-
[CODE]

C:\lab_files\Lesson_6>javac TwoThread.java
TwoThread.java:11: getName() in TwoThread cannot override getName() in java.lang
.Thread; overridden method is final
public final String getName()
^
TwoThread.java:16: setName(java.lang.String) in TwoThread cannot override setNam
e(java.lang.String) in java.lang.Thread; overridden method is final
public final String setName(String Name)
^
2 errors
[/CODE/]
Does this mean that I cannot therefore use methods setName() & getName()
in a class that is subclassed of Thread, and am therefore best off using call().
and forgetting about using them in my program OneThread, or TwoThread - can
someone tell me, so's I (we) can stop spending any more time on this conundrum.
Thank you for your previous advice, and for any forthcoming advice (if you
still feel like talking to me, that is).
Yours
Mary Dawson.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Mary, the end tag of a code block only has one /: [/code]


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4168
    
  21

Does this mean that I cannot therefore use methods setName() & getName()
in a class that is subclassed of Thread, and am therefore best off using call().
and forgetting about using them in my program OneThread, or TwoThread - can
someone tell me, so's I (we) can stop spending any more time on this conundrum.
Thank you for your previous advice, and for any forthcoming advice (if you
still feel like talking to me, that is).


It does not mean you cannot use the methods, it means that you can not OVERRIDE the methods. Two different concepts. You definitely CAN and SHOULD use them.

The first thing you should do is read the API for Thread: Thread's API Reference. You should note when reading that page that there is a constructor for Thread like: Thread(String name) . If you read the documentation for that constructor it tells you what that parameter means (which you should be able to guess).

What does this mean? Why not make your TwoThread constructor look like this:

Let the super class' (Thread's) constructor take care of assigning the name for you. don't define any setName()/getName() methods, because you aren't allowed, and call getName() from the run() method.

Of, if you really wanted to call a setName() method from your constructor, don't make one of your own, just use the one Thread already gives you:

Again, don't define any setName()/getName() methods in TwoThread.

This is why and how you 'extend' methods in Java. You get all the benefits of the super class (it manages Names for you, for example) and get to make changes that are appropriate to the task you want (override the run() method). Less work for you (no maintenance of the Name variable) and more power to the Thread (it does what you want it to).


Steve
Mary Dawson
Ranch Hand

Joined: Feb 08, 2009
Posts: 36
Dear Steve Luke,
Thank you for your advice, in accordance with which I changed program TwoThread to:-


Giving:-



I have done ‘overidding’ previously, but it seems its one thing to do something in a lesson, but another to actually apply it.
Thank you for your help.
Yours
Mary Dawson.










Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4168
    
  21

Mary Dawson wrote:
I have done ‘overidding’ previously, but it seems its one thing to do something in a lesson, but another to actually apply it.
Thank you for your help.


I am glad you got things worked out. Applying lessons to real-world scenarios can be tricky, but it helps to learn to read error messages and being able to track down where the errors come from and why. For example your error messages you got were saying:
TwoThread.java:11: getName() in TwoThread cannot override getName() in java.lang.Thread; overridden method is final


This tells us a lot of information. From Left to Right:
1) The error occurs in the TwoThread.java file
2) The error occurs on line eleven of that file
3) The error message is "getName() in TwoThread cannot be override getName() in java.lang.Thread; overridden method is final" - which is the heart of the error output.

The 'overridden method is final' tells you that you are not allowed to override that particular method, because the maker of Thread defined it as final - which by definition means it can not be overridden.

So reading your error messages carefully will help you solve your problems.

Good luck with your studies.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Rob Prime wrote:Mary, the end tag of a code block only has one /: [/code]

And apparently is case sensitive - lowercase only. Didn't know that myself to be honest...
 
 
subject: How can I get a run() method to return a string variable, when run() method is required to be void ?
 
Similar Threads
problem while displaying multipage tiff image in applet
Need practical example of wait() and notify()
threads problem
not at all able to understand what is happening in this program