Java OCA OCP Practice Question 1223

Question

Given the following class, which line of code when inserted below would prevent the class from compiling?

public class Main { 
        public static void water() {} 
        public void get() { 
           // INSERT CODE HERE 
        } 
} 
  • A. water();
  • B. this.Main.water();
  • C. this.water();
  • D. Main.water();


B.

Note

Options A and D are equivalent and would allow the code to compile.

They both are proper ways to access a static method from within an instance method.

Option B is the correct answer.

The class would not compile because this.Main has no meaning to the compiler.

Option C would still allow the code to compile, even though it is considered a poor coding practice.

While static members should be accessed in a static way, it is not required.




PreviousNext

Related