Course contentsShow
← Java
Lesson 711 of 2,87017. Access Control in DepthFree lesson

Import Statements Do Not Grant Access

Clarify that importing a class does not change visibility rules; access modifiers still apply.

Import Statements Do Not Grant Access

What You'll Learn

You'll learn that importing a class into your file does not change or override Java's access control rules—visibility is still governed by access modifiers alone.


The Core Misunderstanding

Many beginners assume that adding an import statement somehow "unlocks" access to classes or members from other packages. This is not true.

An import statement is purely a typing convenience. It lets you use a class's simple name (like Scanner) instead of its fully qualified name (like java.util.Scanner). That's all.

Access to a class or member is determined entirely by its access modifier (public, protected, package-private, or private)—never by whether you imported it.


The Analogy

Think of import like adding a contact's nickname to your phone. Just because you can type "Mom" instead of her full name doesn't mean you get permission to access her bank account. Permission is determined by her security settings, not your contact list.


What This Means in Practice

  • If a class is package-private in another package, importing it won't make it accessible—you'll get a compiler error.
  • If a method is private in an imported class, you still cannot call it from your code.
  • If a field is protected and you're not in the same package or a subclass, importing the class doesn't help.

The import statement only reduces typing; it doesn't grant privileges.


Key Takeaway:
Importing a class is just a shortcut for using its name—it does not change or bypass access control rules enforced by access modifiers.