Skip to content

OCP exam prep: packages and imports

☕ Java 17 · 🎓 OCP 1Z0-829 · Organizing Your Code with Packages and Imports

Optional page

This page is for readers preparing the Oracle Certified Professional Java SE 17 Developer exam (1Z0-829). The concepts are all you need otherwise.

Four traps to watch for

Trap 1 — A wildcard doesn't import sub-packages

import com.netflix.*; doesn't bring in com.netflix.content.Movie. Each sub-package needs its own import.

Trap 2 — Redundant imports are legal

Importing the same class twice, or a class from java.lang, compiles fine. It's useless, not wrong.

Trap 3 — package comes first

Only comments and blank lines may come before package. Then imports, then the class.

Trap 4 — Same simple name: use the full name

Two single-type imports of classes named Movie don't compile. Import one and write the other's fully qualified name.

Practice questions

1. Which statement is true?

1: import com.netflix.content.*;
2: package com.netflix.platform;
3: public class StreamingPlatform { }

A. It compiles
B. Line 1 doesn't compile
C. Line 2 doesn't compile
D. Line 3 doesn't compile

Answer

C. The import on line 1 is valid on its own. The package statement on line 2 comes after it, which is no longer allowed: package must be first.

2. Which statements are true? (Choose all that apply.) Assume com.netflix.content exists.

package com.netflix.user;
import com.netflix.content.*;
import java.lang.String;
public class User { }

A. It doesn't compile
B. The java.lang.String import is redundant
C. The wildcard also covers com.netflix.content.extras
D. Removing the String import changes the behavior

Answer

B only. The order is correct, so it compiles. java.lang is imported automatically, so that import is redundant and removing it changes nothing. A wildcard never covers sub-packages.

← Back to the series · All 1Z0-829 pages