Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see the picture. I think b is also a right option, am I right?
ME27.png
[Thumbnail for ME27.png]
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

(Note: I notified the author about this errata item)
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

Hope it helps!
Kind regards,
Roel
 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can add anything to String class since it's a final class.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.lang.String.*; is correct because a class can contain a nested class.

 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3819
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Anilprem wrote:Nested classes don't need importing

But you can import them if you so wish, for example, instead of using Map.Entry in your code, you may decide to import java.util.Map.Entry or java.util.Map.* and use Entry in your code instead.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3819
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bishara wrote:
Paul Anilprem wrote:Nested classes don't need importing

But you can import them if you so wish, for example, instead of using Map.Entry in your code, you may decide to import java.util.Map.Entry or java.util.Map.* and use Entry in your code instead.

That is true.
 
Wagner Fonseca
Greenhorn
Posts: 7
Hibernate Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Do is also correct said that a class can contain static member class?

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 an invalid (non-existing) package and/or an invalid (non-existing) class and/or an invalid (non-existing) nested class, you'll get a compiler error. Examples:
1/ import java.laang.*;
2/ import java.lang.Map;
3/ import java.lang.String.Builder;

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)
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"!

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic