Do we need forward declarations in Java? Predict output of the following Java program. // filename: Test2.java // main() function of this class uses Test1 which is declared later in // this file class Test2 { public static void main(String[] args) { Test1 t1 = new Test1(); t1.fun(5); } } class Test1 { void fun(int x) { System.out.println("fun() called: x = " + x); } } Output: fun() called: x = 5 The Java program compiles and runs fine. Note that Test1 and fun() are not declared before their use. Unlike C++, we don’t need forward declarations in Java.
Read full article from Do we need forward declarations in Java? | GeeksforGeeks
No comments:
Post a Comment