I was going through the concept of hoisting in JavaScript where all function and variable declarations are hoisted before any execution takes place and this is the reason why a function is available before its actual declaration part.
Got me wondering how exactly it worked in Java.
Consider the following code:
package declarationOrder;
public class Main {
int num = init();
int init() {
return 5;
}
}
How exactly is the method init() available for a call before its declaration part is reached?
Consider the other example:
package declarationOrder;
public class Main {
int num1 = num2; // compiler error
int num2 = 5;
}
How is it that the order of declaration of the variables plays a role here?
Why and how is the method treated differently?