Instance or static method ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Instance or static method ?

how can we decide to declare a method instance or static in Java ?

31st Mar 2017, 5:54 PM
alpha6572
alpha6572 - avatar
2 Answers
0
Instance method is the part of a class and they are used to manipulate instance variables of a class, In order to call instance method of a class one must have to creat the object of the class. So instance method is specific to object of a particular class.. every object one copy of instance method is created. In case of static method, or specifically a class method. You can directly call static method of a class without creating any object that means it is kind of a shared method and using class name you can directly call the static method without bothering about any object creation, So it depends, if you dont want to create unneccesory object of a class then u can go with static method of that class. Hope it helps, Thank you :)
1st Apr 2017, 11:31 AM
Sbk0103
Sbk0103 - avatar
+ 10
Declare a method as static if you want to call it without creating an object. If you need an object of the containing class anyway, it is useless most of the times ^^ Actually it depends on what your method should do.
31st Mar 2017, 6:45 PM
Tashi N
Tashi N - avatar