What is the result?

Given:

public class Runner {

public static String name = “unknown”;

public void start() {

System.out.println(name);

}

public static void main(String[] args) {

name = “Daniel”;

start();

}

}

What is the result?

Given:

public class Runner {

public static String name = “unknown”;

public void start() {

System.out.println(name);

}

public static void main(String[] args) {

name = “Daniel”;

start();

}

}

What is the result?

A.
Daniel

B.
Unknown

C.
It may print”unknown”or”Daniel”depending on the JVM implementation.

D.
Compilation fails.

E.
An exception is thrown at runtime.

Explanation:
Thecompilation fails at line start();

Exception in thread “main” java.lang.RuntimeException: Uncompilable source code – non-static method start() cannot be referenced from a static context



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Fredy

Fredy

D because non-static method start() cannot be referenced from a static context

pavan

pavan

Object has to be created before calling start();

start() is instance method. D is correct ans.

Compile Error