In order to invoke a method on a object via reflection, the only way I know how to do it is like so:
Object o = ...;
Method m = o.getClass().getMethod("methodName",null);
Object x = m.invoke(o,null);
Why doesn't Java have a getMethods method in the Object class? (As well as getSuperClass, getFields, etc, etc).
So we could just do something like:
Object x = o.invoke("methodName",null);
Why not? I assume this is for performance reasons.
(Also as a side note. In English, it makes more sense to say 'subject invokes object', so that would be in programming terms, object invoke method. But with Java we get 'method invoke on object'. Glad I could confuse you today.)
Object#getMethodsexisted, then it would also have to return aMethodobject corresponding to itself (presumably as part of aCollection<Method>or aMethod[]).Method.class.getMethods()o.methodsName(arguments). This way compiler can do his job and protect us from many errors. In case we don't know methods name while creating code and we want to invoke method which name will be passed by user we can useo.getClass().getMethod("methodName",null).invoke(object,arguments). Only improvement here is length of that code.