+ 1
Help me with function signatures. Thank you :)
Why does my below code doesn't work when I remove the static keyword from the function sayHello()? https://code.sololearn.com/cB86VZ9HvCU4/?ref=app
5 ответов
+ 6
That's what static do. You don't need instances to call static methods. You declare an instance of MyClass to call non-static methods.
Something like this
new MyClass().sayHello();
+ 2
If a method doesn't have the 'static' modifier, you would need to create an instance of the class (myClass) in order to call the sayHello() method.
myClass x = new myClass();
x.sayHello() // prints Hello World.
+ 1
Rishi Technically, in the same class scope.
+ 1
If you do not make the method static , then you must create an instance of the class.
If the method is static , you can call it without creating an instance of the class.
In your code, the method is called without creating an instance of the class. Therefore, the method must be static.
That's how I understood this moment.
0
CarrieForle ooh, what happens here is that, one static function is calling another static function without using the class name because it's in the same scope. Am I right?