Static Imports Flashcards

1
Q

What is a static import and how is it different from a regular import ?

A

A static import is used to import static members from another class and it is different from a regular import because the regular import is used to
import classes
while the static import is used to import members (static members) and allow you to reference the members imported directly without specifying the class they belong to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give me a characteristics of a static import !

A

The characteristic of a static import is that it allows you to use directly the members imported without specifying their class names

EXAMPLE :

import static java.lang.Math.PI;
import static java.lang.Math.pow;

public class Circle {
public static void main(String[] args) {
double radius = 5;
double area = PI * pow(radius, 2); // No need to qualify PI and pow with Math class
System.out.println(“Area of the circle: “ + area);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give examples for all the scenarios of how static members are imported (write the code and run it) !

A

All the ways static members can be imported in java are :
- importing a specific member using its specific name
- importing all static member of a class using a wildcard
and that’s that
.
**EXAMPLE 1 : Importing a specific member ** :

import static java.lang.Math.PI;

public class MyClass {
public static void main(String[] args) {
double radius = 5;
double area = PI * radius * radius; // Using PI directly without specifying Math class
System.out.println(“Area of the circle: “ + area);
}
}

EXAMPLE 2 : importing all static members of a class using a wildcard

import static java.lang.Math.*;

public class MyClass {
public static void main(String[] args) {
double radius = 5;
double area = PI * pow(radius, 2); // Using PI and pow directly without specifying Math class
System.out.println(“Area of the circle: “ + area);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give me all the ways the exam will try to trick you with examples !

A

The most common pitfalls of using static imports are :

  • Using a static import to import a class instead of static members of the class
  • Changing the order of words, instead of ‘import static’ typing ‘static import’
  • Trying to reference an imported static member using the class it belongs to without importing that class

EXAMPLE :

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
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give a common static method invokation conflict you may encounter !

A

When you import two static members with the same name from different classes and attempt to reference one of them in the code without specifying its belonging class, it will result in a compilation error due to ambiguity.

HINT :
To resolve this ambiguity, you need to refer to the static member using its fully qualified name (including the class name) in your code.

EXAMPLE :

import static statics.A.TYPE;
import static statics.B.TYPE;

public class MyClass {
public static void main(String[] args) {
/* Resolve ambiguity by using the fully qualified name */
System.out.println(statics.A.TYPE);
System.out.println(statics.B.TYPE);
}
}

COUNTER-EXAMPLE :

import static statics.A.TYPE;
import static statics.B.TYPE;

public class MyClass {
public static void main(String[] args) {
/* This will result in a compilation error due to ambiguity */
System.out.println(TYPE);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly