Can I have a Java method have params but doesn't require an arguement to be passed in? -


so have program need call method 2 items in object. thought, if have if user passes in nothing arguement...

x.getitem(); 

it work , return 1 of 2 items.

but if wanted specific item...

x.getitem(0); or x.getitem(1); 

is possible in java? didn't want 0 random , 1 , 2 default...because confusing read later.

it sounds want 2 methods:

private a[] = new a[2]; public getitem() {     return a[new random().nextint(1)]; } public getitem(int index) {     return a[index]; } 

given parameters different 2 methods, can use same name (because compiler can distinguish 1 calling depending on actual parameters).

if implement fakey optional parameter, single method more complex - have both ways of looking return value plus logic decide parameter had been omitted. 2 methods nice, clean solution.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -