How to Use Variables in One Method into Another Method

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.

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 do I use variables in one method into another method?

I'm assuming you are new to Java, because it seems you aren't familiar with the concept of fields (i.e. you can put variables outside methods).

public class YourClass {
static String p1;
static String p2;

public static void main(String[] args)
{
System.out.println("Welcome to the game of sticks!");
playerNames();
coinToss();
}

public static void playerNames()
{
Scanner input = new Scanner(System.in);
System.out.println();

System.out.print("Enter player 1's name: ");
p1 = input.nextLine();

System.out.print("Enter player 2's name: ");
p2 = input.nextLine();

System.out.println();
System.out.println("Welcome, " + p1 + " and " + p2 + ".");
}

public static void coinToss()
{
System.out.println("A coin toss will decide who goes first:");
System.out.println();
Random rand = new Random();
int result = rand.nextInt(2);
result = rand.nextInt(2);
if(result == 0)
{
System.out.println(p1 + " goes first!");
}
else
{
System.out.println(p2 + " goes first!");
}
}

}

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)

Referencing a variable from another method

Usually you'd pass it as an argument, like so:

void Method1()
{
var myString = "help";
Method2(myString);
}

void Method2(string aString)
{
var myString = "I need ";
var anotherString = myString + aString;
}

However, the methods in your example are event listeners. You generally don't call them directly. (I suppose you can, but I've never found an instance where one should.) So in this particular case it would be more prudent to store the value in a common location within the class for the two methods to use. Something like this:

string StringA { get; set; }

public void button1_Click(object sender, EventArgs e)
{
StringA = "help";
}

public void button2_Click(object sender, EventArgs e)
{
string b = "I need ";
string c = b + StringA;
}

Note, however, that this will behave very differently in ASP.NET. So if that's what you're using then you'll probably want to take it a step further. The reason it behaves differently is because the server-side is "stateless." So each button click coming from the client is going to result in an entirely new instance of the class. So having set that class-level member in the first button click event handler won't be reflected when using it in the second button click event handler.

In that case, you'll want to look into persisting state within a web application. Options include:

  1. Page Values (hidden fields, for example)
  2. Cookies
  3. Session Variables
  4. Application Variables
  5. A Database
  6. A Server-Side File
  7. Some other means of persisting data on the server side, etc.

How to pass variables from one method to another?

The methods hwpoints, midTerm and finalScore all return an int value which you are not keeping

You need to do some thing like

int hwp = hwpoints ();
int mt = midterm ();
int fs = finalScoare ();

then you can pass these variable into courseScore as

courseScore (fs, mt, hwp);

Note

In this code

int testWeighted = (finalsScore * (testWeight / 100));

you are going to be undertaking integer division.

See Int division: Why is the result of 1/3 == 0?

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.



Related Topics



Leave a reply



Submit