What is the output of the below program?
class Demo {
void foo(Integer i) {
int k = i;
System.out.println(k);
}
public static void main(String h[]) {
new Demo.foo(100);
}
}
Before you scroll down, pick your answer:
- 100
- Compile time error
- Runtime error
- None
The correct answer is: 2. Compile time error
The program does not compile. The error is in the main method, on this line:
new Demo.foo(100);
Why this line fails
The new keyword is only for creating objects. It must be followed by a class name and a constructor call. The syntax is:
new ClassName(arguments)
When the compiler sees new Demo.foo(100), it reads Demo.foo as a class name. But Demo.foo is not a class. It is a method inside the class Demo. The compiler cannot find a class called Demo.foo, so it throws:
error: cannot find symbol
new Demo.foo(100);
^
symbol: class foo
location: class Demo
This is a compile-time error, not a runtime error. The program never gets a chance to run.
Why the answer is not 100
If the code compiled and ran, the output would be 100. The method foo takes an Integer, unboxes it to int, and prints it. But the code never compiles, so 100 is never printed.
Why the answer is not runtime error
Runtime errors happen after the program compiles and starts executing. Here, the compiler rejects the code before any class file is produced. Nothing runs.
Why the answer is not none
"None" would mean the program compiles but produces no output. That is not the case here. The program does not compile at all.
How to fix this program
You have two correct ways to call foo.
Fix 1: Create an object, then call the method
class Demo {
void foo(Integer i) {
int k = i;
System.out.println(k);
}
public static void main(String h[]) {
new Demo().foo(100);
}
}
Output: 100
new Demo() creates the object. .foo(100) calls the method on that object. The dot separates the object creation from the method call. That is the correct syntax.
Fix 2: Make foo static and call it on the class
class Demo {
static void foo(Integer i) {
int k = i;
System.out.println(k);
}
public static void main(String h[]) {
Demo.foo(100);
}
}
Output: 100
If foo is static, you do not need an object. Demo.foo(100) calls the static method directly on the class. No new keyword needed.
The rules behind this error
| Syntax | Meaning | Valid? |
|---|---|---|
new Demo() |
Create an object of class Demo |
Yes |
new Demo().foo(100) |
Create an object, then call foo on it |
Yes |
Demo.foo(100) (if foo is static) |
Call a static method on the class | Yes |
new Demo.foo(100) |
Compiler reads Demo.foo as a class name, which does not exist |
No — compile error |
The rule: new must be followed by a class name and a constructor call. You cannot chain a method call directly after new ClassName without parentheses. The parentheses are what tell the compiler "this is a constructor call, now I can call a method on the result."
One more thing: autoboxing and unboxing
The method signature is void foo(Integer i). The argument is 100, which is an int. Java automatically converts the int to an Integer. That is autoboxing.
Inside the method, int k = i; converts the Integer back to an int. That is unboxing.
This part of the code is fine. The only error is the new Demo.foo(100) line. Do not let the autoboxing distract you from the real issue.
Common variations interviewers ask next
| Variation | Result | Why |
|---|---|---|
new Demo().foo(100); |
Compiles, prints 100 |
Object is created, method is called correctly |
Demo d = new Demo(); d.foo(100); |
Compiles, prints 100 |
Same as above, but stored in a reference variable |
Demo.foo(100); with foo static |
Compiles, prints 100 |
Static method called on the class |
Demo.foo(100); with foo non-static |
Compile error | Non-static method needs an object |
new Demo.foo(100); |
Compile error | Invalid syntax, as explained above |
Where to go next
You now know why new Demo.foo(100) is a compile error, how to fix it, and the difference between static and non-static method calls.
If you remember one thing, remember this: new creates an object. A dot calls a method on that object. You cannot mix them without parentheses. The compiler reads Demo.foo as a class name and fails because no such class exists.
If you want to go deeper, try compiling the original code and read the full error message. Then try the two fixes and confirm both print 100. The difference between new Demo().foo(100) and Demo.foo(100) (when foo is static) is exactly the difference between object-level and class-level method calls. That distinction shows up in every Java interview.
No comments:
Post a Comment