Exam Permalink:
https://www.jobilize.com/java-certification-questions
Question Permalink:
https://www.jobilize.com/how-to-call-java-static-method
Question 81 / 297:  What is the result of compiling and running the following program?
public class Tester {

public static void print(Integer arg){

System.out.print("Integer");

}

public void print(int arg){

System.out.print("int");

}

public static void main(String[] args) {

print(new Integer(3));

}

}

A  int
B  Integer
C  RuntimeException is thrown at run time
D  Compilation error
<< First < Previous Next > Last >>
Explanation:

Class Tester has two methods with the name print(), that both accept an Integer argument.

Which one is called in the main()? Is it the static or the non-static print()?

Calling static or non-static members from inside a static block has rules.

You can call a static method inside a static block by either using:

1. no instance of the parent class of the method (if in the same class): print()

2. an instance of the parent class of the method : new Tester().print();

3. the name of the parent class of the method : Tester.print();

But to call a non static method inside a static block you can only do that through an instance of the parent class of the method.

new Tester().print();

In our example , the static method main() is calling directly print() using NO instance of the class Tester, which means main() is calling the static method print().

Quest for all: if we wrote inside main():

new Tester().print(new Integer(3));

What is the output?

Exam Home Page
https://www.jobilize.com/java-certification-questions

Oracle Certified Professional Java Programmer

Author:

Access: Public Instant Grading

Ask
Savannah Parrish
Start Exam
Copy and paste the following HTML code into your website or blog.
<iframe src="https://www.jobilize.com/embed/java-certification-questions" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>