dcsimg
How can I create a method which takes one or more ...
28 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 
Results  11 to 20 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:   Anonymous  
Posted On:   Monday, February 26, 2001 10:42 AM

You can use a Vector or an Array if you know the max no of parameters that ur method can accept.

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

Posted By:   Darren_Hobbs  
Posted On:   Monday, February 26, 2001 08:32 AM

If you know in advance what the different parameter combinations will be, you could simply overload your method (ie. write several implementations with different signatures). What I sometimes do is implement the method with the most parameters, and write 'adapter' methods that take fewer parameters, and fill in the blanks with default values, or null if appropriate. eg:



public void setFullName(String newTitle, String newForename, String newMiddlename, String newSurname) {
this.title = newTitle;
this.forename = newForename;
this.middlename = newMiddlename;
this.surname = newSurname;
}

public void setFullName(String newSurname) {
setFullName("Mr//Mrs", "", "", newSurname);
}


If you don't know what parameters you will be getting, the only real way to handle that is to add them to Hashtable or HashMap and pass that in as a parameter to your method:


Hashtable myHash = new Hashtable();
myHash.put("forename","John");
myHash.put("surname","Smith");

myMethod(myHash);


Within myMethod you can then iterate through the hashtable contents using an Enumeration or an Iterator (see the API documentation for more details).

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

Posted By:   Luigi_Viggiano  
Posted On:   Wednesday, February 21, 2001 01:22 AM

You cannot. The thing you can do is to use Overloading feature, and chain methods with default value if they are not specified:

...
void myMethod(int par1) {
myMethod(par1, 10);
}

void myMethod(int par1, int par2) {
System.out.println("params: " + par1 + " " + par2);
}
...


Bye,

Luigi.

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

Posted By:   khaal_geigh  
Posted On:   Tuesday, February 20, 2001 02:44 PM

Hello Mr. Wong !!

Optional parameters on Java can be implemented using, I think, 2 ways:

- Method overwriting: If you know exactly all cases on which your method is called, your method can be declared many times, each of them declaring parameters you need. Take account, otherwise, that a method signature includes only type of your formal parameters, so you cannot use overwriting by changing only parameter names. e.g.
...
private String mymethod(String mystr)
private String mymethod(int myint)
private String mymethod(int myint,boolean a)
...

- Encapsulation of parameters: Note that first solutions is not valid when you know number and type of parameters on runtime, because you cannot assure you have declared proper method. Better solution is to use as a parameter some 'container' of parameters, like a Hashtable or a Vector. e.g.
...
private String mymethod(Hashtable a);
...

Good luck !!

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

Posted By:   Anonymous  
Posted On:   Thursday, February 15, 2001 11:34 PM

You can use an vector (of Objects) as argument. From here you may send all different types. If you want to send primitives you must use the wrapper classes.

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

Posted By:   Ravi_Van  
Posted On:   Wednesday, February 14, 2001 02:47 PM

You cannot. You need to explicitly create one function for each set of parameters that you want it to accept.
-Ravi

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

Posted By:   Vikram_Lele  
Posted On:   Wednesday, February 14, 2001 12:43 AM

Java doesn't support optional parameters. Though we can easily achive the same effect in the following way :


For example, lets say we have a method of a class


void foo(int param1, int Param2) {
// some implementation
}



If you want Param2 to be optional, you will have to provide another implementation of foo, with only one parameter.


void foo(int param1) {
foo(Param1, DEFAULT_PARAM2);
}


This will effectively allow you to have optional params with minimum coding effort.


Regards,

-Vikram

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

Posted By:   Chandra_Patni  
Posted On:   Saturday, February 10, 2001 07:13 PM

Currently, there is no satisfactory way of creating a method which takes one or more optional parameters. However, Java programming language allows us to achieve the approximate effect in the following three ways. For more details of each method, please refer to Section 2.4 of JSR 0065.


  1. Massive overloading

  2. Passing object arrays


  3. Chained methods


A new facility called Concise Object-Array Literals (JSR 0065) is likely to be added in Java language to orvercome the limitation of passing variable number of arguments to a method/constructor. This proposed facility at JCP allows a method to take one or more optional parameters in nearly compact and ideal way. However, it requires a minor change in the language. For example in the following example demonstrates how C style printf() can be implemented in Java just by "boxing" the arguments in the caller expression. Boxing can be done by { and } tokens.

int a, b, c;
Stdio.printf("Time is %d hr %d min %d sec
", {a, b, c}); // note the boxing of a, b, c between {}
// .... rest of the code goes here

class Stdio {
public static int printf(String format, Object[] args) {
// implementation of printf
}
}

See also New I/O APIs for the Java Platform (JSR 0051)

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

Posted By:   andreas_erlach  
Posted On:   Saturday, February 10, 2001 02:46 PM

IMO there is no need for optional parameters in Java. It's quite easy to create an array, a collection, or any other object and to pass it as parameter. Et voilà!

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

Posted By:   khaal_geigh  
Posted On:   Thursday, February 8, 2001 12:57 AM

* Polymorphism alternative:

If you know exacly how many parameters you can receive, you can override a method definition by passing each time different parameters, varying either number or type.
Note that polymorphism will resolve which method to use by matching number and type of your parameters.
That is, e.g.:
...
public String getName(boolean sorted);
public String getName();
public String getName(int index);
...

* Encapsulation alternative:

But, what about if we don't now at compile time what parameters we can receive ? You can always encapsulate parameters inside an object. Then, this object, with naming lookup ability (e.g. a Hashtable or whatever object you can define) can contain your parameters. Note that formal parameters can vary dynamically. Also, you'll need to put some semanthic content to the container, because you lose the formal parameters check (number and type of the formal parameters).
Results  11 to 20 of 28  « Prev 1 2 3 Next » 
About | Sitemap | Contact