Is there anything wrong with the Mala Gupta Question27 in mock exam?

nemo zou wrote:Please see the picture. I think b is also a right option, am I right?

Yes, you are right! Answer b is also correct. The import statement import java.lang.String.*; is valid: this statement let's you use the class name of all classes in the package java.lang.String (which of course doesn't exist, because it's a class).

It's weird that statement is allowed, because if you try with a package which doesn't exist, you get a compiler error. This code snippetproduces this compiler errorThe same applies to a custom non-existing package as well.

And if you add something other than a star after java.lang.String, you'll get a compiler error as well. Example:

I don't think you can add anything to String class since it's a final class.

Joe Bishara

Ranch Hand

Posts: 175

17

posted 1 year ago

1

import java.lang.String.*; is correct because a class can contain a nested class.

Paul Anilprem

Enthuware Software Support
Ranch Hand

Posts: 3819

10

posted 1 year ago

Joe Bishara wrote:import java.lang.String.*; is correct because a class can contain a nested class.

Nested classes don't need importing (not talking about static imports) if the containing class is already imported.
import java.lang.String.*; is valid because it is. The compiler just makes sure that String is either valid a package or a valid class in lang. It generates an error if it is neither.

Joe Bishara wrote:import java.lang.String.*; is correct because a class can contain a nested class.

You are absolutely correct! Didn't think about that one for a moment (in my defense: it was already 3am when I made that post, so not sharp-witted anymore ). Have a cow for this great remark!

So, now we can have a few simple rules:
1/ the import statement for all classes in a package: import java.util.*; 2/ the import statement for a specific class in a package: import java.util.Map; 3/ the import statement for all nested classes in a class: import java.util.Map.*; 4/ the import statement for a specific nested class in a class: import java.util.Map.Entry;

When you use the star (*) - only allowed at the end - the compiler only checks if package/class exists, not if the package is empty nor if the class has nested classes. Examples:
1/ import java.*.String; (compiler error)
2/ import java.lang.String.*; (no compiler error)

Hope it helps!
Kind regards,
Roel

(Disclaimer: nested classes and java.util.Map are not on the OCAJP exam, they are used for educational purposes only)

Wagner Fonseca wrote: Do is also correct said that a class can contain static member class?

That import statement is not a correct static import, so it won't let you use static members without using the class name.

The correct static import for all static members of the String class
The correct static import for only the valueOf class method of the String class and the MAX_VALUE class variable of the Integer class
Without a static import the code must be changed accordingly, otherwise you'll get a compiler error

If you use a static import for a static member of a class, the static member must exist; otherwise you'll get a compiler error. Examples:
1/ import static java.lang.String.parse; (compiler error)
2/ import static java.lang.RuntimeException.*; (although RuntimeException doesn't have any static members, no compiler error)

Watch out: although the language feature is called "a static import", the correct Java syntax is "import static"!