Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whizlab, static import question

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Which of the following set of import will allow the following
class to compiler and run without errors?




Select 2 options:

[A]
import java.lang.System.*;
import static xyz.Test.Utilities;
import static xyz.Test.Inner;

[B]
import static java.lang.System.out;
import static xyz.Test.*;

[C]
import static java.lang.System.out.*;
import static xyz.Test.*;

[D]
import static java.lang.System.*;
import static xyz.Test.Utilities;
import xyz.Test.Inner;

[E]
import static java.lang.System.*;
import static xyz.Test.Utilities;
import static xyz.Test.Inner;

[F]
import static java.lang.System.out;
import static xyz.Test.Utilities;
import xyz.Test.Inner;



Given Answer is B, E
I say "D" and "F" are also correct.

The rationale behind Whizlab answer is "By default class enclosed enum are static"

Please correct me!!!


Regards,
cmbhatt
[ April 16, 2007: Message edited by: Chandra Bhatt ]
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

actually we can import a static nested class and enum declared in a class by writing both the following statements

why this is happening i dont know
import static xyz.Test.*; //both Inner enum and static class Utilities are getting imported,and other static variable or method if decalred in Test class are also imported.

same is happening i we write
import xyz.Test.*; //both Inner enum and static class Utilities are getting imported,but if there is some other static method or variable in Test class it will not be imported.

Why so?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saran,


Saran says :
same is happening i we write
import xyz.Test.*; //both Inner enum and static class Utilities are getting imported,but if there is some other static method or variable in Test class it will not be imported.


Nay, I would have to disagree with you here, you just forget the importance
of static import. When you do static import only static members of that class are imported. If not so (as you say) what would be difference between static import and plain import statements.



Regards,
cmbhatt
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know the difference between static and normal import.

but i am saying that for static nested classes and enums,using ne king of import is not making difference why so?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saran,

To be or not to be! this is the question!!!

Let us see what others say!


Regards,
cmbhatt
[ April 16, 2007: Message edited by: Chandra Bhatt ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic