Given: 10. package com.sun.scjp; 11. public class Geodetics { 12. public static final double DIAMETER = 12756.32; // kilometers 13. } Which two correctly access the DIAMETER member of the Geodetics class? (Choose two.)
-
import com.sun.scjp.Geodetics;public class TerraCarta {public double halfway(){ return Geodetics.DIAMETER/2.0; } }
-
import static com.sun.scjp.Geodetics;public class TerraCarta {public double halfway() { return DIAMETER/2.0; } }
-
import static com.sun.scjp.Geodetics. *;public class TerraCarta {public double halfway() { return DIAMETER/2.0; } }
-
package com.sun.scjp;public class TerraCarta {public double halfway() { return DIAMETER/2.0; } }
Option A uses a regular import and accesses the static field using the class name (Geodetics.DIAMETER), which is valid. Option C uses a static import with wildcard (import static com.sun.scjp.Geodetics.*) which allows accessing DIAMETER directly without the class prefix. Option B is incorrect because static import requires either a wildcard or a specific static member name. Option D is incorrect because DIAMETER is not accessible without class qualification in a different package.
To access the DIAMETER member of the Geodetics class, we need to understand the different ways to access static members.
Option A) import com.sun.scjp.Geodetics; public class TerraCarta { public double halfway(){ return Geodetics.DIAMETER/2.0; } }
This option is correct because it imports the Geodetics class and accesses the DIAMETER member using the syntax Geodetics.DIAMETER.
Option B) import static com.sun.scjp.Geodetics; public class TerraCarta { public double halfway() { return DIAMETER/2.0; } }
This option is incorrect because it tries to import the Geodetics class using the static keyword, but it does not specify which member of the class to import. Therefore, the line return DIAMETER/2.0; will cause a compilation error because DIAMETER is not recognized.
Option C) import static com.sun.scjp.Geodetics. *; public class TerraCarta { public double halfway() { return DIAMETER/2.0; } }
This option is correct because it imports all the static members of the Geodetics class using the * wildcard. Therefore, the line return DIAMETER/2.0; will correctly access the DIAMETER member.
Option D) package com.sun.scjp; public class TerraCarta { public double halfway() { return DIAMETER/2.0; } }
This option is incorrect because it does not import the Geodetics class, so the line return DIAMETER/2.0; will cause a compilation error because DIAMETER is not recognized.
Therefore, the correct answers are Option A and Option C.