✅ Method declaration, outside Main Vs inside main
Hello guys, consider the following code:
At first, I wrote the DisplayRandomNumbers() method outside the Main method but I got an error telling me that I can't call the DisplayRandomNumbers from a static context. Can someone explain why I got this error and why if I put the method inside the main method itself, it works fine. What's happening behind the scenes here?
14 Replies
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
Ah, we assume that every method created inside main is also static in nature? even though we didn't use the static keyword ?
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
yep, I know from Java, that static means we don't need an instance of a class (an object) to call a method
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
ah yeah
my bad
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
yep sorry
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
if we use top level statement, here we must explicitly use the keyword static?
I tried it using top level statements, work fine without using the static keyword, but as a good practice, we should include it?
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
yeah I see
any function declared inside another function, like DisplayRandomNumbers() inside name, "inherits" from the main context, like static access ?
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
yep I see, thanks !