Home > Software engineering >  How to pass variable from one parameterized method to another?
How to pass variable from one parameterized method to another?

Time:07-14

How do I send the variable from main method to the Method A, and what Parameter should I pass within A(), in method B.

Code:

public class MethodCall {
    
    public void A(String c) {
        
        System.out.println(c);
    }
    
    public void B() {
        A(); // What parameter do I pass here. method B is dependent on A
        
    }
    @Test
    public void D() {
        B();
        MethodCall mc = new MethodCall();
        mc.A("Hello");
    }

}

CodePudding user response:

In method A you expect one parameter of type String therefore you have to provide a parameter for String can be just an empty String "" or null.


Another option depending on your use case; you could use varargs:

public void a(String... varargs) {...}

In this case it's possible to give zero or multiple parameters of type String. So this works:

a("Hello World");

this:

a();

But also this:

a("Hello", "World");

Or you could just pass all parameters necessary for A through B:

public class MethodCall {
    public void A(String c) {
        System.out.println(c);
    }
    
    public void B(String c) {
        A(c);
    }
    @Test
    public void D() {
        MethodCall mc = new MethodCall();
        mc.B("Hello World");
        mc.A("Hello");
    }

}

CodePudding user response:

You should pass the String parameter for within A(), in method B.

public class MethodCall {

    public void A(String c) {
    
        System.out.println(c);
    }

    public void B() {
        A("Pass the String parameter"); 
    
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MethodCall mc = new MethodCall();
        mc.A("Hello");
        mc.B();
    }
}

CodePudding user response:

when you passing the String type as a parameter its mean you should use to same data type when you giving the value to the parameter. you cant use any other types like Integer, float etc. you just need to pass the String value to the given parameter.

When you are using non-static method it cannot call directly. so use static keyword to call the method in directly.

ANSWER-

public class MethodCall {
    
    public static void A(String c) {
        
        System.out.println(c);
    }
    
    public static void B() {
        A("HI "); //you can put `null` or `empty`
        
    }

    public static  void main(String[] args) {
        B();
        MethodCall mc = new MethodCall();
        mc.A("Hello");
    }

}

CodePudding user response:

I am not so sure about your question but here is my take:

    public class MethodCall {

    public void A(String c) {

    System.out.println(c);
    }

    public void B(String s) {
    A(s); //If you want to call A() here, then you would have to pass a variable in the parameters of A(), as A() is a parameterized method by definition.

    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    MethodCall mc = new MethodCall();
    mc.A("Hello"); //This part is right, that is how you pass value of a non static function through main method.
    }
  •  Tags:  
  • java
  • Related