+ 1

What is the difference between a method starting with static and one without static like:

public void hello() and static void hello()

31st Jan 2017, 12:33 PM
Srijit Nair
Srijit Nair - avatar
3 Respostas
+ 2
Static voids are accessible using a class itself. Non-static voids are accessible using declared object of the class. Example: public class A { public A(){}; public static void foo1() { cout << 1; } public void foo2() { cout << 3; } } int main(args) { A obj = new A(); A.foo1(); obj.foo2(); ret 0; } Output: 13 Hope I helped somehow. Good luck!
31st Jan 2017, 2:21 PM
Refy
Refy - avatar
+ 2
If any variable or method declared by using "static" keyword, then this variable or method is static variable or static method like static void hello() If any variable or method not declared by using "static" keyword, then this variable or method is non-static(instance) variable or non-static(instance) method like void hello() You can use any one of the static or instance in your code. If you want to call static member ,then use class_name.member example:: demo.x , demo.hello() If you want to call instance member ,then you have to create object or instance. example:: class_name reference_name=new class_name(); reference_name.member; Like :: demo d=new demo(); d.x; d.hello();
1st Feb 2017, 4:39 PM
Taufique Sekh
Taufique Sekh - avatar
0
Isn't static means" execute first"
4th Apr 2017, 12:18 PM
Tahreem khalil
Tahreem khalil - avatar