java Method class

hi,

why java.lang package has class called Method.

what are other classes there in that and what is purpose of those classes.

when do we need to use them

please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
Please spend time reading this tutorial:
0
dpearsonCommented:
The Method class is part of the "reflection" API.  You rarely need to use any of this API in normal programming.

It tends to come up when you want to be able to do something like call a method - but you have the name of the method represented as a String.

It's the difference between:

MyClass myClass = new MyClass() ;
myClass.hello() ;

and

MyClass myClass = new MyClass() ;
Method helloMethod = getMethod(myClass, "hello") ;   <-- See how the name of the method is passed in as a string here
helloMethod.call() ;

(NOTE: That's not real code - just giving you an idea of what reflection is all about).

As I say - it's unlikely you need this, so probably not something you should be diving into just now.

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
right.

i also see methods like setShortDescription. I wonder what is use of args here and also methods like setShortDescription. please advise

     Class cl = Xyz.class;
         Class args[] = { };
         Method cToF = cl.getMethod("cT", args);
         MethodDescriptor cTDesc = new MethodDescriptor(cToF);
         cTDesc.setShortDescription("modify c to f");
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

dpearsonCommented:
That's moving into even more obscure corners.  Java has a concept of java beans that in some cases can be modified by external tools.  An example of this is using a debugger to connect into a running process and change the current logging level.

This API (I believe - I've never used it) supports that sort of operation.

This is way way off the beaten path.  I would recommend not delving into this area until you've mastered the basics - you will get very lost.

Doug
0
gudii9Author Commented:
helloMethod.call() ;

call() is other method on the hello() method?

please advise if there is any link with sample code to understand this atleast at high level.
0
dpearsonCommented:
Sure the Java tutorials cover this in depth:
https://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html

And yes "helloMethod.call()" is another way to call to the hello() method.  You get an object which represents the method itself (i.e. an object that represents a piece of program code) and then you call that object.

I see looking at the tutorial the actual method name is "invoke":
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#invoke-java.lang.Object-java.lang.Object...-

So I should have said "helloMethod.invoke()" in my pseudo code.

As I mentioned earlier - this is advanced stuff.  You could program professionally for 10 years in Java and never need to use this stuff - so proceed with caution :)

Doug
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.