+ 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

20th Aug 2021, 6:10 AM
Rishi
Rishi - avatar
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();
20th Aug 2021, 7:47 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 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.
22nd Aug 2021, 2:30 AM
Chuks AJ
Chuks AJ - avatar
+ 1
Rishi Technically, in the same class scope.
20th Aug 2021, 10:14 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 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.
21st Aug 2021, 3:54 AM
Evseev Anton
Evseev Anton - avatar
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?
20th Aug 2021, 10:00 AM
Rishi
Rishi - avatar