You are asked to create a method that accepts an array of integers and returns the highest value from that array Given the code fragment:
Which method signature do you use at line n1 ?
A. public int findMax (int [] numbers)
B. static int[] findMax (int max)
C. static int findMax (int [] numbers)
D. final int findMax (int [])
Option C
we can’t call instance methods from static method without object creation
if we add A “public int findMax(int [] numbers)”, compiler will inform what should be corrected (method defined from main static method should be static as well):
#javac Test38.java
Test38.java:4: error: non-static method findMax(int[]) cannot be referenced from a static context
int max = findMax(numbers);
^
1 error
correction per answer “C” will allow to compile without errors:
# javac Test38.java
Answer C:
Calling the findMax() method is happening from a static context without instantiating the class. Therefore, the called method should be static.
C
1) main method is static and findMax method is declared outside the main method and it is used inside the main method so it must be static.
2) we can put the public at the beginning of the findMax method or not. It depends
Answer is C
Lot of answers are incorrect. How can you guarantee that we will pass. Correct answer is C in above question number 24