0

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 want to 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?

1
  • 3
    Seriously, if you are this green don't start with android. Start with Plain Old Java and when you have some background to it begin playing with android. Commented Dec 23, 2011 at 9:44

7 Answers 7

4
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
}
Sign up to request clarification or add additional context in comments.

2 Comments

but how is it able to read that the String is from postData() method??
it is not reading the string, you are explicitly passing it to the examplePayment() method.
3

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);
}

Comments

2

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) {
    .
    . 
    .
}

Comments

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

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


}

Comments

1
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
}

Comments

1

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
    } 

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.