• 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

Correct import statement? from Khalid Mughal Review Questions- 4.3

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
4.3 Given the following code:
// (1) INSERT ONE IMPORT STATEMENT HERE
public class RQ700_10 {
public static void main(String[] args) {
System.out.println(Locale.UK); // Locale string for UK is "en_GB".
}}
Which statements, when inserted at (1), will result in a program that prints en_GB, when compiled and run? Select the two correct answers.
(a) import java.util.*;
(b) import java.util.Locale;
(c) import java.util.Locale.UK;
(d) import java.util.Locale.*;
(e) import static java.util.*;
(f) import static java.util.Locale;
(g) import static java.util.Locale.UK;
(h) import static java.util.Locale.*;

I am not able to differentiate between import and import static. Please help.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the name import static alone should have given you a hint. as the name suggest import static are used to import static members of the class , so that you need not use the name of class while accessing such members. for example when we use System.out.println , out is a static field of System class. instead of writing System.out.println we can use "STATIC IMPORTS"(beware they are called static imports but written as import static). so what you can do is do this

import static java.lang.System.* // this will import all the static members in System class

or

import static java.lang.System.out // the specific member to import.

In the question given below the author has cleverly tried to confused you. UK is a static member of Locale class. You normally access it using Locale.UK(normal import statement specified). in this case you can rule out all the static imports directly. now from amongst the others the correct choices are the one with the proper hierarchy of Locale class which is java.util.* or java.util.Locale hence option (a) and (b) is the correct choice.

option (c) and (d) are clearly not a proper syntax of import . (e) is not valid static import

(f) is also not a valid static import

(g) and (h) are valid static imports but it will give compile error that it cannot find symbol Locale. had you done like this System.out.println(UK); and used one of the option (g) or (h) , the program would have compiled and ran since now you have "statically" imported UK field of Locale class with either option (g) or (h)
 
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think b and g should be correct.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ankita modi. wrote:I think b and g should be correct.



if b is correct as you say , why can't (a) be ???

(g) and (h) are valid static imports but they will result in compiler error since they provide for the static member and not for the class Locale. you can run the program and check for yourself
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:

ankita modi. wrote:I think b and g should be correct.



if b is correct as you say , why can't (a) be ???

(g) and (h) are valid static imports but they will result in compiler error since they provide for the static member and not for the class Locale. you can run the program and check for yourself



Yes,it may be, i didn't check.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
g and h are the correct ones. a is not correct because Locale.UK is a static field in the Locale class. So you must use import static.
 
Mansi Agarwal
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The right answer is (a) & (b).

That's a nice explanation Gurpreet.. Thanks a lot...
 
Mansi Agarwal
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got stuck in one similar question:
Which sequence of import statements, when inserted at (1), will result in the code
compiling, and the execution of the main() method printing:
|Giffy||Tiffy||Jpeggy||Jpeggy||Jpeggy||Jpeggy|
Select the three correct answers.
(a) import p3.Util;
import p3.Util.Format;
import static p3.Util.print;
import static p3.Util.Format.*;
(b) import p3.Util;
import static p3.Util.Format;
import static p3.Util.print;
import static p3.Util.Format.*;
(c) import p3.*;
import static p3.Util.*;
import static p3.Util.Format.*;
(d) import p3.*;
import p3.Util.*;
import static p3.Util.Format.*;

-Answer is a,b,c.
I dont understand how import static p3.Util.Format.* works ?? As we use ststic import to access static members only.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you already know that enums are the solution to java's requirement for enumerated data types. also the enum constants are nothing but public final static fields. so you can use static imports to import them. this thing is explained very good in kb6 book. you can refer that if you have it
 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Format is implicitly static. JPEG, GIF, TIFF are implicity static too.
 
Mansi Agarwal
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Gurpreet!!

I did read that. Was missing this enum thing.
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic