3 Static Imports Flashcards

1
Q

Explain why these won’t compile

The exam will try to trick you with misusing static imports.

1: import static java.util.Arrays; // DOES NOT COMPILE
2: import static java.util.Arrays.asList;
3: static import java.util.Arrays.*; // DOES NOT COMPILE
4: public class BadStaticImports {
5: public static void main(String[] args) {
6: Arrays.asList(“one”); // DOES NOT COMPILE
7: } }

A
  1. Line 1 tries to use a static import to import a class. Remember that static imports are only for importing static members (methods or variables).
    2.Line 3 tries to see if you are paying attention to the order of keywords. The syntax is import static
    and not vice versa.
    3.We imported the asList method on line 2. However,
    we did not import the Arrays class anywhere. This makes it okay to write asList(“one”);
    but not Arrays.asList(“one”);. Should be written asList(“one”);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly