Results 1 to 6 of 6

Thread: Passing optional parameters in java

  1. #1
    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.

  2. #2
    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.

  3. #3
    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?

  4. #4
    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());
    }

  5. #5
    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;
    	}
    }

  6. #6
    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.

Similar Threads

  1. Passing by reference in Java
    By Amy Adams in forum Software Development
    Replies: 5
    Last Post: 06-04-2010, 11:17 AM
  2. Message Passing in Java
    By rashmi_ay in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 10:33 PM
  3. Java & Oracle: Passing Date
    By garfield1 in forum Software Development
    Replies: 5
    Last Post: 04-01-2010, 12:30 PM
  4. Problem Passing Data Dynamically in Java
    By Kushan in forum Software Development
    Replies: 3
    Last Post: 26-03-2009, 02:26 PM
  5. Passing arrays as parameters in Vb.Net
    By kaartik in forum Software Development
    Replies: 2
    Last Post: 07-01-2009, 12:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,060,217.97496 seconds with 17 queries