J jonmullen Platinum Member Jun 17, 2002 2,517 0 0 Jan 8, 2004 #1 How do I define and argument in a method as optional, that way if it is not supplied it is ok. In php it would be something like this function($required,$optional="option") { print $required; if($optional != "option") { print $optional; } }
How do I define and argument in a method as optional, that way if it is not supplied it is ok. In php it would be something like this function($required,$optional="option") { print $required; if($optional != "option") { print $optional; } }
M Mitzi Diamond Member Aug 22, 2001 3,775 1 76 Jan 8, 2004 #2 How about: Class myClass(paramType paramName) { if (paramName == null) { blah } ... } Call method with: myClass(null); I'll go try it out now...
How about: Class myClass(paramType paramName) { if (paramName == null) { blah } ... } Call method with: myClass(null); I'll go try it out now...
M Mitzi Diamond Member Aug 22, 2001 3,775 1 76 Jan 8, 2004 #3 public class test { public static void main(String[] args) { // String myString = "hello"; String myString = null; myMethod(myString); } static void myMethod (String myParam) { if (myParam == null) { myParam = "default"; } System.out.println("myParam: " + myParam); } } Try that. Note that you still need to pass a parameter to the method but it is acceptable to simply pass null (i.e. myMethod(null);). There may be a way to specifically identify that a parameter is optional but I'm not aware of it, sorry.
public class test { public static void main(String[] args) { // String myString = "hello"; String myString = null; myMethod(myString); } static void myMethod (String myParam) { if (myParam == null) { myParam = "default"; } System.out.println("myParam: " + myParam); } } Try that. Note that you still need to pass a parameter to the method but it is acceptable to simply pass null (i.e. myMethod(null);). There may be a way to specifically identify that a parameter is optional but I'm not aware of it, sorry.
K Kilrsat Golden Member Jul 16, 2001 1,072 0 0 Jan 8, 2004 #4 The nice way to do it is to have two methods: static void myMethod() { //just calls the other version of the method, with null values myMethod(null); } static void myMethod(String test) { //actually does stuff }
The nice way to do it is to have two methods: static void myMethod() { //just calls the other version of the method, with null values myMethod(null); } static void myMethod(String test) { //actually does stuff }
A AmigaMan Diamond Member Oct 12, 1999 3,644 1 0 Jan 8, 2004 #5 Originally posted by: Kilrsat The nice way to do it is to have two methods: static void myMethod() { //just calls the other version of the method, with null values myMethod(null); } static void myMethod(String test) { //actually does stuff } Click to expand... what he/she said.
Originally posted by: Kilrsat The nice way to do it is to have two methods: static void myMethod() { //just calls the other version of the method, with null values myMethod(null); } static void myMethod(String test) { //actually does stuff } Click to expand... what he/she said.
M Mitzi Diamond Member Aug 22, 2001 3,775 1 76 Jan 8, 2004 #6 ^^^ What he said! Didn't think of overloading the function