dcsimg
How can I create a method which takes one or more ...
28 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 
Results  21 to 28 of 28  « Prev 1 2 3 Next » 

Posted By:   king_hw
Posted On:   Monday, January 22, 2001 12:10 AM

How can I create a method which takes one or more optional parameters?

Re: How can I create a method which takes one or more...

Posted By:   Ryan_Breidenbach  
Posted On:   Tuesday, February 6, 2001 08:37 AM

In Java, you can not explicitly create a method where any of the parameters are optional. For example, if you had a class declared as:

public class SomeClass {
public staic void doSomething(Object param1, Object param2) {
// body
}
}

You could not call SomeClass.doSomething(someObject). The number of parameters in a method you are calling must explicitly match the signature of method in the class (or one of it's superclasses/interfaces) on which you are calling the method.


That said, there are options in Java for accomplishing the use of optional parameters. There are three options that seem the most obvious solutions:



  1. Declare a method with all the possible parameters, and pass in null for the optional parameters. So, the method call above would look like SomeClass.doSomething(someObject, null)


  2. Instead of having multiple parameters, have one object that encapsulates the possible parameters. Then the class' method above would look like:

    public class SomeClass {
    public staic void doSomething(ParamWrapper wrapper) {
    // body
    }
    }

    Where ParamWrapper looks like:

    public class ParamWrapper {
    private Object param1;
    private Object param2;
    public getParam1() { return param1; }
    public getParam2() { return param2; }
    // rest of body
    }

    Now, all parameters are wrapped in a single "query" object.


  3. And I think this is the best OO approach Overload the method. This OO feature is perfect for this situation. Then, your class could look like:

    public class SomeClass {
    public staic void doSomething(Object param2) {
    // body
    }

    public staic void doSomething(Object param1, Object param2) {
    // body
    }
    }

    Now, the second parameter becomes optional. However, this assumes that the first parameter is always mandatory. If more the one parameter is optional and each has the same data type in the method signature (such as two optional String parameters), then the method call becomes ambiguous. In this case, method overloading is not an option and I would recommend using option #2. Also, if the number of parameters (optional or not) grows to large (e.g 12 parametes), I would also suggest option #2.

Re: How can I create a method which takes one or more...

Posted By:   Michael_Rohde  
Posted On:   Saturday, February 3, 2001 10:13 AM

You have to create an extra method for each combination ("signature") of parameters desired. But the methods can, of course, rely on each other:



void createCircle(int x, int y, int radius) {

  // do something

}


void createCircle(int x, int y) {

  createCircle(x, y, 100);

}

Re: How can I create a method which takes one or more...

Posted By:   Nicholas_Whitehead  
Posted On:   Tuesday, January 30, 2001 02:27 PM

Something along the lines of:



public void optArgFunction(Object[] optArgs) {

.

.

.

.

}



Simple, but it works.

Re: How can I create a method which takes one or more...

Posted By:   Chandra_Patni  
Posted On:   Tuesday, January 30, 2001 02:28 AM

In Java there is no construct like ... operator in C where you can pass optional parameters. You can, however, achieve the same effect by overloading a method.

Re: How can I create a method which takes one or more...

Posted By:   Arvind_Prabhakar  
Posted On:   Monday, January 29, 2001 02:03 PM

Dear Honwai,



In my knowledge, there is no special construct in Java language that allows for passing optional parameters. However, there are some workarounds that are worth considering:


  • Use of Collections: In most cases, certain types of collections may be used for passing in a list of objects that are actually the optional parameters. The drawback with this approach is that you will have to use objects to represent your optional parameters - which means that for every case where you want to pass a primitive type, you will have to pass the corresponding wrapper like java.lang.Integer for a normal int argument. The advantages you have with this approach are many. You could have ordered collections indicating the order of the parameters passed if that is of any significant consequence to your application. You could have Map constructs that associate descriptive keys to the arguments, which may help provide extra information to process these parameters and so on.

  • Lazy Method Invocation: This is a much less known strategy which involves the repetitive calls to secondary helper methods to set the state of the object, followed by the actual method call with no arguments whatsoever. So you could call the setArgument(Object arg) method as many times as you want (or any other specialized strain of this method), before calling the method that will deal with all these arguments.


Hope this helps.



Regards,

Arvind Prabhakar

Re: How can I create a method which takes one or more...

Posted By:   Francois_Nadeau  
Posted On:   Friday, January 26, 2001 06:40 PM

You need to declare a methode signature at compile time, so you will not be able to create a methode which takes a variant amount of parameters. However, I would suggest ussing an array of parameters, for instance:


public void bla( Object[] args ) {
    if( args != null )

      for( int i = 0 ; i < args.length ; i++ )

        //do something with .... args[i] ....


}



This would be used in such a way:


object.bla( new Object[] { x , y , z } ) ;


Where x,y and z are the parameters which you want to pass on. This will allow a user to pass 0 to X arguments.

Re: How can I create a method which takes one or more...

Posted By:   Anonymous  
Posted On:   Wednesday, January 24, 2001 02:25 AM

You can't use optional parameters in Java.

The closest thing you can do is to overload the method:


void myMethod(int number, String name) {
// code goes here
}

void myMethod(int number) {
myMethod(number,"jGuru");
}

Re: How can I create a method which takes one or more...

Posted By:   Pratik_Roy  
Posted On:   Tuesday, January 23, 2001 11:05 AM

You will have to create multiple methods with different signatures, having same method name. Java supports Function overloading. So you can write the functions (methods) with various parameters (or no parameter), and they would be called automatically, based on the no. of parameters, type of parameters, and order of the parameters. This is usually known as the "signature" of the function. And there is no limitation on the different signatures that can be created for a method (function).
Results  21 to 28 of 28  « Prev 1 2 3 Next » 
About | Sitemap | Contact