简体   繁体   中英

Passing a String from one method to another method

Sorry for this newbie simple question, but I need to pass a String from one method to another method and now I'm a bit clueless

the String values (callbackURL & donationId) I wanna pass are inside this method:

public void postData() {
.
.
.
.
            String callbackURL = tokens.nextToken();
            String donationId = tokens.nextToken();

    } 

and I want to pass the values to another method, let's say it's called private examplePayment() . How am I able to perform that? Thank you

public void postData(){
.
.
.
.
      String callbackURL = tokens.nextToken();
      String donationId = tokens.nextToken();
      examplePayment(callbackURL, donationId);
}

private void examplePayment(String callback, String donationid){
      //deal with your callback and donationid variables
}

You pass values to another method by declaring the parameters in the method signature, for example:

private void examplePayment(String callbackURL, String donationId) {
  //your logic in here
}

Then you can call it in your code like so:

public void postData() {
   String callbackURL = tokens.nextToken();
   String donationId = tokens.nextToken();
   examplePayment(callbackURL, donationId);
}
public void postData() {
            String callbackURL = tokens.nextToken();
            String donationId = tokens.nextToken();

           examplePayment(callbackURL,donationId);
    } 
private void examplePayment(String callbackURL,String donationId){


}

You mean you want to pass the strings to the function as arguments?

public void postData() {
   .
   .
   . 
   String callbackURL = tokens.nextToken();
   String donationId = tokens.nextToken();
   examplePayment(callbackURL, donationId);
}
.
.
.
private void examplePayment(String callbackURL, String donationId) {
    .
    . 
    .
}
public String postData() {
   String callbackURL = tokens.nextToken();
   String someID = tokens.nextToken();
   this.examplePayment(callbackURL, someID);
}

OR
private examplePayment(String callbackURL, String someID){
    //   DO whatever you want with callback and someID
}

Why dont you create method with arguments eg create a method like the following

private examplePayment(String callbackURL, String donationId){

...do your work

}

while calling you can pass arguments in postData method

public void postData() {
.
.
.
.
            String callbackURL = tokens.nextToken();
            String donationId = tokens.nextToken();
            examplePayment(callbackURL, donationId);// call the mthod
    } 
public class StringTest{    
    public void postData() {
        String callbackURL = "abc.cde.com";
        String donationId = "1233445";
        examplePayment(callbackURL,donationId);
    } 

    private void examplePayment(String pStrCallbackURL , String pStrDonationId){
        System.out.println("Callback url"+ pStrCallbackURL );
        System.out.println("DonationId "+ pStrDonationId );
    }
}

Now if you want to enable your examplePayment method to know which method is sending the strings , then add another method parameter , say "pCallingMethod" .

The method will look like

private void examplePayment(String pStrCallbackURL , String pStrDonationId , pStrCallingMethod){        
    System.out.println("Callback url"+ pStrCallbackURL );
    System.out.println("DonationId "+ pStrDonationId );
            System.out.println("Calling Method"+ pStrCallingMethod);
}

Pass the CallingMethodName as parameter to this method whenever you call examplePayment() .

This can be a solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM