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
Post a Comment