File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes get a value from a void method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "get a value from a void method" Watch "get a value from a void method" New topic
Author

get a value from a void method

ahmed mego
Greenhorn

Joined: Feb 10, 2013
Posts: 6
How can I get the variable "n" in the void method:
i know that void return null but there are any solution for that

I want to get the value of "n" to use it in this method:
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2229
    
    7

Are both methods in the same class? If so, can this variable become an instance variable?

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11152
    
  16

As written, you can't. the variable n doesn't exist once you leave the method.

so, you can either:

a) change the method to actually return a value
b) make n a class variable that gets set inside the method
c) make the call to get the value where you need it and can use it.

quite frankly, the method NoOfProcess, as written, is basically worthless.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

ahmed mego wrote:How can I get the variable "n" in the void method:
i know that void return null but there are any solution for that


void method can't even returns null. It returns nothing.


Do not wait to strike till the iron is hot; but make it hot by striking....
 
jQuery in Action, 2nd edition
 
subject: get a value from a void method
 
Similar Threads
jcomboboxes linked to textarea displays
passing string
My program forgets about previously stored elements
Right click popup menu
to print output in textarea