Jessica M.
Jessica M.

Reputation: 1459

Parameter passing and method calling

I have a question about parameter passing. In this example methodTwo wants to call methodOne but only use the x, and y values and not the Color color. When I try to do this i get a error in Eclipse "the method methodOne(double x, double y, Color color) in the type 'example class name' is not applicable for the arguments (double, double))"

Can methodTwo not call another methodOne if it does not use exactly all of the arguments of methodOne?

private void methodOne (double x, double y, Color color){
   statements...;
  }

private void methodTwo (x, y ){
  methodOne(x, y);
  statements...;
}

Upvotes: 0

Views: 124

Answers (4)

Alpesh Gediya
Alpesh Gediya

Reputation: 3794

you need to use all the parameter to call method1. (Order of parameters and type of parameter are also important)

If you do not have third parameter you can use method1 as

private void methodTwo (x, y ){
  method 1(x, y, null);
  statements...;
}

Upvotes: 1

JDGuide
JDGuide

Reputation: 6525

In your code as below :-

  private void methodTwo (x, y ){
      methodOne(x, y);  //Now this will show error , because parameter not matching
      statements...;
    }

If you do not want to pass 3rd parameter then it will show error. So, you have to pass 3rd parameter and for your purpose you can pass null , being you are not using the 3rd parameter in the function definition.

Ist Solution :-

private void methodTwo (x, y ){
      methodOne(x, y,null); 
      //statements...;
    }

2nd solution you can overload this methodOne with 2 parameter like below :-

private void methodOne(double x, double y, Color color){
   //statements... same job;
  }

private void methodOne(double x, double y){
   //statements...same job;
  }

Now when you call the methodOne method with 2 parameter as follow :-

private void methodTwo (x, y ){
  methodOne(x,y); // Now the overloaded method will call
  //statements...;
}

Upvotes: 1

eManna
eManna

Reputation: 2482

Can method 2 not call another method 1 if it does not use exactly all of the arguments of method 1?

it can but you have to override method 1 like this:

    private void method 1 (double x, double y, Color color){
       statements...;
      }

    private void method 1 (double x, double y){
       statements...;
      }

    private void method 2 (x, y ){
      method 1(x, y);
      statements...;
    }

Upvotes: 0

wchargin
wchargin

Reputation: 16037

Method names have to be one word. You also need to provide the last parameter.

private void method1 (double x, double y, Color color){
   statements...;
  }

private void method2 (x, y ){
  method1(x, y, someColorOrNull);
  statements...;
}

From the JLS, section 3.8 "Identifiers":

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.

Identifier:
    IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral

IdentifierChars:
    JavaLetter
    IdentifierChars JavaLetterOrDigit

JavaLetter:
    any Unicode character that is a Java letter (see below)

JavaLetterOrDigit:
    any Unicode character that is a Java letter-or-digit (see below)

Upvotes: 0

Related Questions