How to Use a Variable of One Method in Another Method

Using variable from one method in another method

Aside: This is a java specific question, not an object-oriented question.

The crux of your question is about java scopes. The variable response is defined in the GetActivityById method. That method has a { and a }. That is the scope of the variable. What scope of a variable means, is that the variable is only visible - meaning, it only "exists" [1] after it was defined, [2] until the end of the scope (the curly brackets) it was defined in.

However, if you call a method, methodB from methodA, then methodB will have its own scope, which is separate from the scope of methodA which is the method calling it.

The method bodyResponse only has access to variables defined in its scope or passed into it as a parameter. So, to share a variable from one method to another, one way to do that is to pass it as a variable. You did this already with the exe variable. So, do the same for the response variable to fix this error:

public static void GetActivityById(){

Response response=Authentication.Creds("www.randomurl.com");

System.out.println("The extracted thing is: " + bodyResponse("name", response));//note I added *response*

}
public static String bodyResponse(String exe, Response response){//note I added *response*


JsonPath jsonPathEvaluator = response.jsonPath();
String bodyExample = jsonPathEvaluator.get(exe).toString();
return bodyExample;
}

Using variable from one method in another method in Python

You're calling account_deposite before self.balance has been initialized. Attributes like this should be declared and initialized in __init__. Try this

class Account_Balance(Account_General):
def __init__(self, first_name, middle_name, last_name, PIN, DoB, city, address, phone_number, email):
super().__init__(first_name, middle_name, last_name, PIN, DoB, city, address, phone_number, email)
self.balance = 0.0

def account_balance(self):
self.balance = 10.0
return self.balance

def account_deposite(self, amount):
self.amount = amount
self.balance += self.amount

How can we pass variables from one method to another in the same class without taking the help of parent class?

You can declare b method with two parameters, as following example:

public class Dope
{
public void a()
{
String t = "my";
int k = 6;

b(t, k);
}

public void b(String t, int k)
{
System.out.println(t+" "+k);
}

public static void main(String Ss[])
{

}
}

How to access variables from one method into another method in ruby?

Local variables are scoped to the local context. You cannot access them from arbitrary other places in the code; that's the whole point of them being local.

If you want to access a variable globally across the whole program (which is, 99.9% of the time, very bad practice), then you need to declare it as a global variable.

In ruby, global variables are declared by naming them with a $ symbol, e.g. $num_1.


Alternatively, you could pass the variables into the other method. For example:

def request_op_num
puts "Please enter your first number"
num_1 = gets.to_f
puts "Please enter the operator you would like to use (+,-,*,/)"
op = gets
puts "Please enter your second number"
num_2 = gets.to_f

answer(num_1, num_2, op)
end

def answer(num_1, num_2, op)
if op == '+'
puts num_1 + num_2
elsif op == '-'
puts num_1 - num_2
elsif op == '*'
puts num_1 * num_2
elsif op == '/'
puts num_1 / num_2
else
puts "wrong operator"
end
end

request_op_num

Or:

def request_op_num
puts "Please enter your first number"
num_1 = gets.to_f
puts "Please enter the operator you would like to use (+,-,*,/)"
op = gets
puts "Please enter your second number"
num_2 = gets.to_f

[num_1, num_2, op]
end

def answer(num_1, num_2, op)
if op == '+'
puts num_1 + num_2
elsif op == '-'
puts num_1 - num_2
elsif op == '*'
puts num_1 * num_2
elsif op == '/'
puts num_1 / num_2
else
puts "wrong operator"
end
end

num_1, num_2, op = request_op_num

answer(num_1, num_2, op)

Passing a variable from one method to another method?

Let your randomInt() return the number and call the function inside of shuffle().

private static void shuffle(Card[] cardArray) {
for (int i = 1; i <= 20; i++) {
int a = randomInt();
int b = randomInt();
Card temp = cardArray[a];
cardArray[a] = cardArray[b];
cardArray[b] = temp;
}
}

private static int randomInt() {
return (int)(Math.random() * 12);
}

This will shuffle your card deck according to the way randomInt() generates the indices

How do i call on a local variable from another method?

In Java there are things called "scopes" between the parenthesis. A variable that is created in one scope cannot be accessed by another scope. An example is the variable that you are using here. What you can do is you can either call the method from another method and get a return value for "user", or you can pass the variable in one scope as a parameter for another method. I will demonstrate with ar mini example below:

public static void scopeOne() {
String myName = "name";
}

public static void scopeTwo() {
System.out.println(myName);
}

This will obviously not work because you cannot access myName from scopeOne in scopeTwo this way. You will get a compile-time error. You could solve this in various ways, but here is one example:

public static void scopeOne() {
String myName = "name";
scopeTwo(myName);
}

public static void scopeTwo(String myName) {
System.out.println(myName);
}

You could pass the variable to the method as an argument and make the variable a parameter. That way it can be within the local scope of the method you are attempting to call. Another thing you can do is make the User object variable a class or instance variable and then update it within your methods.

How to call a variable in another method?

First declare your method to accept a parameter:

public void take(String s){
//
}

Then pass it:

public void example(){
String x = "name";
take(x);
}

Using an instance variable is not a good choice, because it would require calling some code to set up the value before take() is called, and take() have no control over that, which could lead to bugs. Also it wouldn't be threadsafe.



Related Topics



Leave a reply



Submit