0

In Java is it possible/correct to call a method before it is declared?

public static void main(String[] args) { hello(); } static void hello() { System.out.println(“hi”); } In the example above, it looks as though the method ‘hello’ was called on the second line before it was declared two lines down. Is it okay?

1st Jan 2021, 2:11 AM
Tommy Mensah
3 Antworten
+ 8
Царь СОБАКА - Догго I My pleasure... 👌 I think many people share the same understanding as your earlier comment and so I thought it would be helpful for me to show a simple demo. 😉
1st Jan 2021, 3:26 PM
David Carroll
David Carroll - avatar
+ 6
Царь СОБАКА - Догго I What do you mean it's not possible in Python? There are two such scenarios that come to mind where similar behavior can exist in Python. First, the order doesn't matter within class methods. Second, a similar main() function can be created to simulate a single entry point as seen in Java. Here's a little demo I put together to demonstrate: https://code.sololearn.com/c4AAaU6pWLwa/?ref=app
1st Jan 2021, 7:18 AM
David Carroll
David Carroll - avatar
+ 3
Yes it is possible in Java.
1st Jan 2021, 2:48 AM
Sonic
Sonic - avatar