+ 1

why do we need to use objects to declare methods

we can use methods without objects so what is the advantages of using objects.what does objects do.edite me if lam wronge

30th Nov 2019, 7:14 AM
‎يوسف سعد‎
‎يوسف سعد‎ - avatar
2 ответов
+ 1
Methods can be invoked without objects only if they are declared static. But static methods can also be invoked using objects of the class. Now coming to your question, consider you are trying to create a friends class and what are the objects? Your friends. Let us say your friends are A, B, C etc. Now see all your friends are of course humans but don't you think they all have different characteristics and behaviours of their own. This is the reason you need methods to define those behaviours for different objects. Each object is unique in its own way.
30th Nov 2019, 8:40 AM
Avinesh
Avinesh - avatar
+ 2
you don't need object for declare method (in class). if method is static you don't need object to call and use this method. but look here: class Person { String name; int age; } ... Person p1 = new Person(); p1.name="John"; Person p2 = new Person(); p2.name="Stuart"; Person p3 = new Person(); p3.name="Anton"; this you cant do without objects, because if you have 1000 persons it is not good idea write new class for each one.
30th Nov 2019, 7:54 AM
zemiak