So in java you cannot have duplicate method names with different return values ​​and params?

Is it possible to have two methods with the same name, but with different parameters and return types in Java? It seems like this would be a good way to generalize a simple getter and setter. You can do this with constructors, why not use regular methods? eg

why not be able ..

int getVal() {

return int;
}

boolean getVal() {

return true;

}

setVal(int a) {
}

and

setVal(boolean a) {

}
+5
source share
8 answers

Because then the compiler could not figure out:

setVal (getVal ());

should it call the bool or int version?

+11
source

What do you expect if I call:

getVal();

? : . , , , .

, , ( ).

+15

, , , , () (), , ?

java.sun.com

Java , Java . , , ( , " " ).

, .

, , apar t.

, , .

+6

Java Virtual Machine, , .

, Java .

+5

- . - :))

+4

, . /, . , Java... . , .

+3

- .

, . , throws ( , ).

0

Java (http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#8.4.2):

( ) .

, .

0

All Articles