• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

import static question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

i come across this question:

given:
package sun.scjp;

public enum Color { RED, GREEN, BLUE}

package sun.beta;
//insert code here

public class Beta{
Color g= GREEN;
public static void main(String [] args){
System.out.println(GREEN);}
}
}

The class Beta and the enum Color are in different packages.
Which two code fragments, inserted individually at line 2 of the Beta declaration,
will allow this code to compile? (choose 2)

a. import sun.scjp.Color.*;
b. import static sun.scjp.Color.*;
c. import sun.scjp.Color; import static sun.scjp.Color.*;
d. import sun.scjp.*; import static sun.scjp.Color.*;
e. import sun.scjp.Color; import static sun.scjp.Color.GREEN;

My answer is C and D, while the correct answer is C and E.
How? D and E looks the same.. Can anyone give me a clear explanation about this?

appreciate your help
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!!!

The code is compiling with d) and e)

d. import sun.scjp.*; import static sun.scjp.Color.*;
e. import sun.scjp.Color; import static sun.scjp.Color.GREEN;

I don't understand why you have to choos only two.
I think that c, d and e is the correct answer.

Where did you get the code ?
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
question D, is missed why first import does not import enum, the letter does not import a static member, and letter B only imports static, and the letter most correct is C and E which had in the letter E to import correct the member duly enum. These questions are suffered.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ailina n, your name does not conform to the JavaRanch name policy.

Please take a moment to carefully read the policy and change your display name. Note that your name should consist of a first name, space and a second name. A single letter "n" is not an acceptable second name.

Note that conforming to the name policy is not optional. If you do not change your name after multiple warnings, your account might be locked. You have already been warned more than once.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Ailina!

Thanks for changing you user name.

c and d and e compile.


In the aligned version I think it is obvious that all three do their job.

The non-static import is required for line
Color g= GREEN; in Beta

And the static import for line
System.out.println(GREEN);


Yours,
Bu.
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic