+ 1
What is the difference between a method starting with static and one without static like:
public void hello() and static void hello()
3 Réponses
+ 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!
+ 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();
0
Isn't static means" execute first"