Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

get a value from a void method  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are both methods in the same class? If so, can this variable become an instance variable?
 
lowercase baba
Bartender
Posts: 12613
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!