The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Parameter Passing Styles Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Developer Certification (SCJD/OCMJD)
Reply Bookmark "Parameter Passing Styles" Watch "Parameter Passing Styles" New topic
Author

Parameter Passing Styles

David Reck
Ranch Hand

Joined: May 25, 2001
Posts: 37
Could someone please explain to me why it would be better to pass in parameters like this:
java -Dhost=Me -Dport=123 ReservationSystem
Instead of creating your own flags in your program for specific parameters allowing the user to supply the parameters in any order like this:
Java ReservationSystem -HMe -P123
or
Java ReservationSystem -P123 -HMe
Rick Fortier
Ranch Hand

Joined: Jun 04, 2001
Posts: 147
Could someone please explain to me why it would be better to pass in parameters like this:
java -Dhost=Me -Dport=123 ReservationSystem

I don't think this way works. At least it didn't for me. I did get to work:
java -Dmypkg.myclass.myvar=100 ReservationSystem
But just to set variables I could not. Is this working for you?
In my final submission I had any variables to be passed after the class name, not before. However I did have some variables that are used to control my background timer thread to set the frequency.
java -Dsuncertify.fbnserver.FBNServer.TimerLength=20 FBNServer
And I only specified that they could be set in the comments to the class, not in the documentation since this variable is not one of the acceptable parameters we are allowed to pass.
David Reck
Ranch Hand

Joined: May 25, 2001
Posts: 37
It works for me because I have a method that loops through the args parameter of the main method searching for specific flags like -H for host. When a flag is found is stores the attached value like localhos for -Hlocalhost in a class field. So why can we not do it this way? It would allow you to create your own flags and not to us the -D java parameter over and over again.
Rick Fortier
Ranch Hand

Joined: Jun 04, 2001
Posts: 147
Originally posted by David Reck:
It works for me because I have a method that loops through the args parameter of the main method searching for specific flags like -H for host. When a flag is found is stores the attached value like localhos for -Hlocalhost in a class field. So why can we not do it this way? It would allow you to create your own flags and not to us the -D java parameter over and over again.


So do the parameters before AND after the class name show up in args[] of main() ?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
It looks like all this -D stuff is not quite clear. The idea of using command line parameters of the form -Dparameter=value, is that these parameters are read by the virtual machine at startup and stored as system properties, which can later be accessed using the System.getProperty() method. THESE ARE NOT VISIBLE AS COMMAND LINE ARGUMENTS (those you see on the args[] array passed to the main method).
In regards to the choice you decide to make (whether to use system properties or arguments), I personally believe that there isn�t big difference. You are not going to get more points if you do it one way or the other.
Hope this helps!!!
Benjam�n
David Reck
Ranch Hand

Joined: May 25, 2001
Posts: 37
I think you are right about not getting any additional points either way I just want to make sure points are not deducted one of the ways I listed.
Rick,
To answer your question only the parameters listed after the class name show up in the args array.
Akanimo Udoh
Ranch Hand

Joined: Jun 11, 2001
Posts: 48
All the comments on this topic I've looked at are right but we've missed out on one important part - "Coding Standards".
The marking outline for the exam gives this 23 marks in the General Considerations section. So what we should ask is what is the "Most appropriate" way to pass parameters to a java application? Is it using the Java's inbuilt 'system properties'? or is it by passing the parameters to the main(...) function's command line?
I think this is important.
Akanimo.
David Reck
Ranch Hand

Joined: May 25, 2001
Posts: 37
Akanimo,
I think you are correct. I can see good arguments for passing the parameters either way, but since you have to use the -D for the codebase and the policy file it would probably be better to us -D for all parameters just to stay consistent. Besides I reread my assignment and it specifies that "Designs should not involve building new code if the effect can adequately be achieved using facilities in the standard Java packages" and I did have to write a method to parse the args array. Which will be removed if I use -D.
Thanks for your insight.
Dave
 
 
subject: Parameter Passing Styles
 
Threads others viewed
how to get mysql backup using java swing
casting home/remote object
Difference between attributes and parameters
command line parameters
Sql query isssue
MyEclipse, The Clear Choice