Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Pass optional parameters to Methods?  (Read 3252 times)
0 Members and 1 Guest are viewing this topic.
Offline elias4444

Junior Devvie

« Posted 2004-03-02 20:05:30 »

I know there's got to be an easy way to do this, but I can't find it for the life of me.

When I build a new method, I want to be able to make it so that the first parameter is always required, but the second is optional. i.e.:

public void calculate(int firstnum, Object objpass) { something with firstnum.... something with objpass as pertaining to firstnum calculation if objpass exists....

Any way to do this? If I specify the the second parameter in the method parameter list, it requires it when called. Help?

Offline Orangy Tang

JGO Kernel

Medals: 57
Projects: 11

Monkey for a head

« Reply #1 - Posted 2004-03-02 20:09:10 »

Just write a second calculate that only accepts an int, and have it delagate to your actual calculate method to do the actual heavy lifting (with the default arg). Slightly more bulky code, but as far as I'm aware the only way to get C++ style default args.

[ - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline elias4444

Junior Devvie

« Reply #2 - Posted 2004-03-02 20:11:45 »

Murphy's Law strikes again....right after I posted, I found my answer! So, if anything, thank you all for being subjected to a truly Murphy-ized Newbie.  Tongue

For those wondering...when calling the method, you just have to specify somemethod(integerofsomesort, null);

Thank you for those who were so speedy to reply (I've already gotten a reply while composing this message). You folks are fantastic!

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline blahblahblahh

JGO Coder

Medals: 1

« Reply #3 - Posted 2004-03-02 20:23:03 »

...or wait until 1.5 goes gold (it supports method signatures with variable numbers of parameters).

malloc will be first against the wall when the revolution comes...
Offline elias4444

Junior Devvie

« Reply #4 - Posted 2004-03-02 20:27:37 »

...or wait until 1.5 goes gold (it supports method signatures with variable numbers of parameters).

How will THAT work?

Offline rreyelts

Junior Devvie

There is nothing Nu under the sun

« Reply #5 - Posted 2004-03-03 00:48:15 »


How will THAT work?

The term they're using for that feature is "varargs". You can look up the spec for it at It works by letting you annotate a method as accepting a variable number of arguments. The compiler automatically boxes arguments to any method that is tagged with a VARARGS access modifier.

Personally, I preferred concise object array literals, but it's looking too late for that change to take place.

God bless,
-Toby Reyelts

About me:
Jace - Easier JNI:
Retroweaver - Compile on JDK1.5, and deploy on 1.4:
Offline sma

Junior Devvie

« Reply #6 - Posted 2004-03-08 06:33:45 »


How will THAT work?

You can define a method method(type... name) which is equivalent to method(type[] name) and then call it with method(a, b, c) instead of using method(new type[]{a, b, c}). Because Java 1.5 will automatically convert primitive data into instances of Wrapper classes, stuff looks much nicer, for example
format("%1 = %2", "3+4", 3+4);
instead of
format("%1 = %2", new Object[]{"3+4", new Integer(3+4)});

.: Truth Until Paradox!
Offline z.e.r.o

Junior Devvie

Java games rock!

« Reply #7 - Posted 2004-03-09 03:49:31 »


That is neat

Matteo Anelli
.brain -
Offline Abuse

JGO Ninja

Medals: 64

falling into the abyss of reality

« Reply #8 - Posted 2004-03-10 05:25:01 »

More feature rich = less noob friendly

The explicitness (is that a word???) of Java was, IMHO 1 of its benefits.
All the added syntactic sugar is only going to further confuse newbies to the language.

Think about how many Java noobies actually understand what is happening behind the scenes with string concatenation,
And how often the question, 'my String concatenation is realy slow!' is asked...

Variable argument lists  & especially Autoboxing is going to multiply that problem 100fold.
Pages: [1]
  ignore  |  Print  

EgonOlsen (42 views)
2018-06-10 19:43:48

EgonOlsen (22 views)
2018-06-10 19:43:44

EgonOlsen (43 views)
2018-06-10 19:43:20

DesertCoockie (197 views)
2018-05-13 18:23:11

nelsongames (124 views)
2018-04-24 18:15:36

nelsongames (123 views)
2018-04-24 18:14:32

ivj94 (864 views)
2018-03-24 14:47:39

ivj94 (125 views)
2018-03-24 14:46:31

ivj94 (768 views)
2018-03-24 14:43:53

Solater (140 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!