An object is an instance of a class, and it is used to call methods. In our previous post, Post 2: Understanding Methods in Java, we discussed methods. Now, we’ll show how to call those methods through objects.
In this example, the object and methods are created in two different classes:
Breakdown of First1 object1 = new First1();:
At the bottom of the image, there's the output of the code.
'Print of first Method'
Calling a Method:
object1.method1();
Here:
It’s important to note that method1 was defined in the First1 class.
Attaching an image of the first method from the class 'First1' for your reference.
Static Methods:
Static methods are special methods that can be called without creating an object. Since they are linked to the class, not to any instance of the class, we don’t need to create objects to use them.
I am attaching an image for your reference.
In this image, the marked part is the static method.
In this image, the marked part is the call to the static method. And at the bottom of the image you can see the code output.
The above is the detailed content of Post Understanding Objects and Methods in Java. For more information, please follow other related articles on the PHP Chinese website!