Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 05-03-2010
Member
 
Join Date: Dec 2009
Posts: 292
Passing optional parameters in java

Hello,
I have one small question: is it possible to pass parameters optional ways to accordingly using Java? Also if this can not be done in java programming language then what is the alternative that java provides to the user of the language to get the same output. If you know this then please let me know, I am the interested one here. Thanks in advance.
Reply With Quote
  #2  
Old 05-03-2010
Member
 
Join Date: Nov 2008
Posts: 240
Re: Passing optional parameters in java

Hello,
Even I am new to java programming language, no, i do not think it is possible to have parameters optional in the sense you hear, for cons nothing stopping you from passing a table or a collection of Object to simulate this behavior. I guess I am correct here, if not the please guide me with the correct because even i was trying this some time long but was not successful. Hope this will help you.
Reply With Quote
  #3  
Old 05-03-2010
Member
 
Join Date: Nov 2009
Posts: 335
Re: Passing optional parameters in java

Hello,
To continue also nothing stopping you from passing a null pointer to your function. You also have the option to override your method and the decline after several types of calls, and to avoid duplicating the code you can call a private method of your common overloaded methods. I do not understand why do you want to pass the optional parameters in your code, what is the exact reason for the same?
Reply With Quote
  #4  
Old 05-03-2010
Member
 
Join Date: Nov 2009
Posts: 583
Re: Passing optional parameters in java

Hello,
I think you do not need a private method in your code, I do not know your exact need, but if you do not then this may be a solution for you. Just encode method takes the maximum setting, for example, have a look the code.
Code:
Public void method(String pr1, pr2 date, int pr3) {
	/ / This method does all the work 
	// ...
	// ...
}
And then the overload with the version "light" settings in which it just to call it:
Code:
Public void method(String pr1, pr2 Date) {
	method(pr1, pr2, 0);
}
 
Public void method(String pr1) {
	method(pr1, new Date());
}
Reply With Quote
  #5  
Old 05-03-2010
Member
 
Join Date: Nov 2009
Posts: 518
Re: Passing optional parameters in java

Hello,
Yes I agree with the above post, I would like to add to the above a sample demo example, see if this helps you
Code:
class metprm {
	
	String pr1;
Date pr2 = null;
	int pr3 = 0;
	
	/ ** Private Constructor: use the static create method * /
	private metprm(String pr1) { 
		/ / Empty
	}
	
	Public static Create metprm(String pr1) {
		return new metprm(pr1);
	}
	
	Public metprm pr2(Date pr2) {
		this.pr2 = Param2,
		return this;
	}
	
	Public metprm pr3(int pr3) {
		this.pr3 = Param3;
		return this;
	}
}
Reply With Quote
  #6  
Old 05-03-2010
Member
 
Join Date: Dec 2009
Posts: 292
Re: Passing optional parameters in java

Hello,
Thank you for all these responses. Finally, I will certainly legally use outright method overload. In fact my plan was generated automatically by a tool named JavaCC (a parser to do). The method I want to change is called a can anywhere in my program and would not have to rewrite everything. I do not know that this is the perfect solution for the problem, but I think I will carry with this idea. Thanks again for your replies.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Passing optional parameters in java"
Thread Thread Starter Forum Replies Last Post
Passing by reference in Java Amy Adams Software Development 5 06-04-2010 11:17 AM
Message Passing in Java rashmi_ay Software Development 5 25-02-2010 10:33 PM
Java & Oracle: Passing Date garfield1 Software Development 5 04-01-2010 12:30 PM
Problem Passing Data Dynamically in Java Kushan Software Development 3 26-03-2009 02:26 PM
Passing arrays as parameters in Vb.Net kaartik Software Development 2 07-01-2009 12:47 PM


All times are GMT +5.5. The time now is 02:19 AM.