Java OCA OCP Practice Question 288

Question

Which is the most common way to fill in the blank to implement this method?

public class MyClass { 
        private double myField; 
        public static void setBeakLength(MyClass p, int b) { 
            //blank line
        } 
} 
  • A. p.myField = b;
  • B. p['myField'] = b;
  • C. p[myField] = b;
  • D. None of the above


A.

Note

Options B and C do not compile.

In Java, braces are for arrays rather than instance variables.

Option A is the correct answer.

It uses dot notation to access the instance variable.

It also shows that a private variable is accessible in the same class and that a narrower type is allowed to be assigned to a wider type.




PreviousNext

Related