Skip to content
Advertisement

how to pass variables between three different methods in java

hello I need to pass value from method A() to Method C() then after call Method C() inside the method B(). please any one show me the syntax.

for example:

void pay(){
    int amount = 100;
            String payerName = "Enock",
            payerPhone = editphonenumber.getText().toString();
    Api.flutterwavePayment(KuguraActivity.this,Integer.valueOf(amount), payerName,payerPhone,"Kugura ibicuruzwa");

}

I need this payerPhone the next follow method called onActivityResult()

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == RaveConstants.RAVE_REQUEST_CODE && data != null) {
        String message = data.getStringExtra("response");
        if (resultCode == RavePayActivity.RESULT_SUCCESS) {
            Toast.makeText(this, "SUCCESS " + message, Toast.LENGTH_LONG).show();
            // phone number here
        }  
    } 
}

Advertisement

Answer

Firstly I strongly recommend you to gain some teoretical knowledge about java (follow some tutorials, read documentation etc)

As I suppose you want to get some data from previously called method to achive it. There’s no sense in doing such thing because methods are destroyed after usage and they do not store data anymore. You may want to use static fields in your project. Please read carefully this thread: What is the exact meaning of static fields in Java?

One of the way to get the data from method is to create method with returning value. Then you can use returned value as new data or change static field. https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

There’s tutorial about returnig values: https://www.geeksforgeeks.org/return-keyword-java/

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement