Can i call a method defined in a class without creating an object of that class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can i call a method defined in a class without creating an object of that class?

15th Jul 2017, 5:31 AM
Arpit Rastogi
Arpit Rastogi - avatar
4 Answers
+ 2
yes you can, just make it static and call it with a period/dot (.)
15th Jul 2017, 5:34 AM
Deddy Tandean
+ 2
Make the method static.
15th Jul 2017, 5:34 AM
Debakar Roy
Debakar Roy - avatar
0
public class dog{ static void bark(){System.out.println("woof");}/*you have created a block and assigned bark with "woof"*/ main /*calling bark from dog*/dog.bark(); } /*output "woof"*/ /*im learning my self so im assuming this is correct as this is how i see it*/
15th Jul 2017, 10:17 AM
D_Stark
D_Stark - avatar
0
in this case if I only write bark(); instead of dog.bark(); then too output will be same
15th Jul 2017, 10:23 AM
Arpit Rastogi
Arpit Rastogi - avatar