We called the compare() method directly in main() method. Should we not have created an object first, through "Solution solution = new Solution();" and used the compare method like this: solution.compare(3); solution.compare(6); solution.compare(5);
How did we use method compare directly instead of creating an object?
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
J H U X
15 January 2021, 14:56
Instance methods are methods which require an object of its class to be created before it can be called. To invoke a instance method, we have to create an Object of the class in within which it defined.
TL/DR: Instance method(s) belong to the Object of the class not to the class i.e. they can be called after creating the Object of the class.
This is a static method: Static methods as name states defined at the class level and could be accessed on the class name i.e no need of class object creation in order to access/call the static methods..
Hope this helps. Good luck coding!
0
Gellert Varga
10 April 2020, 21:19
If you write this inside the main method:
compare(6);
then with this code you are calling the compare() method.
The method accepts the value "6".
And you have to write commands inside the method, what it should do if this number smaller/larger than 5.
0