-1

I have a use case to use named and optional parameter.I tried to use named parameter as in a tutorial but it doesn't work

My code is

public static void Main(String[] args)
  {
     System.out.println((CalculateBMI(weight= 123, height: 64));
  }
  public static int CalculateBMI(int weight, int height)
  {
      return (weight * 703) / (height * height);
  }

getting Error " weight cannot be resolved to a variable" Please help

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
user1790894
  • 407
  • 3
  • 9
  • 17
  • also the main method is spelled with a non-capital m – jlordo Nov 01 '12 at 09:49
  • 3
    Please post the URL to the tutorial, so we can correct the original author. (This wouldn't even be valid C#, as you've got `weight = ` rather than `weight:`.) – Jon Skeet Nov 01 '12 at 09:49

4 Answers4

3

You're probably reading the wrong tutorial, Java doesn't support neither named nor optional parameters.

See also:

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
2

This is the best you can do:

int weight = 123; 
int height = 64;    
System.out.println((CalculateBMI(weight, height));
duffymo
  • 305,152
  • 44
  • 369
  • 561
2

Java does not support named parameters. Groovy, which compiles to Java byte code does have named parameters. Also you can compile Java source files using Groovy (not recommended as you will not benefit from the features of Groovy).

Ayub Malik
  • 2,488
  • 6
  • 27
  • 42
0

code is:

public static void Main(String[] args) {
    System.out.println(CalculateBMI(123,64));
}

public static int CalculateBMI(int weight, int height) {
    return (weight * 703) / (height * height);
}
Hardik Lotiya
  • 371
  • 3
  • 9
  • 28
  • You could just edit (=bugfix) your answer, and my comments would disappear ;) – jlordo Nov 01 '12 at 10:00
  • @jlordo: The *compiler* won't care that the method is called `main` rather than `Main`. It's a perfectly valid name. It's only when you try to use the class an entry point that it'll be a problem. – Jon Skeet Nov 01 '12 at 12:47
  • @JonSkeet correct, the compiler won't care, but this is obviously a main method that the OP wants to execute. And i doubt he wants to write a `main` method which calls his `Main` method :) – jlordo Nov 01 '12 at 18:06
  • @jlordo: Sure - but if you're going to pick nits, it's at least worth picking them carefully :) – Jon Skeet Nov 01 '12 at 18:08