Calling a function before Defining it in Java

70 views Asked by At

I have noticed that you can call a function in java before defining it, just like Javascript. Now I know that Javascript supports function hoisting because of which it is possible to do so, but what about Java, for ex

public class Sample_class {
    

    public void sample_meth1()
    {
        sample_meth2(); //calling before defining
    }

    public void sample_meth2() //Function Definition
    {

    }
}

The same goes for main method, where you can call a method before it has been declared and defined. Why and How?

I was expecting a compile time error, but it executed successfully.

1

There are 1 answers

0
Hadi Kattan On

In Java, the order of the methods doesn't matter. You can call a method from any other method, even though one method is defined before the other.