PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

Packages in Java

← All stacks

Theory

47/270

Packages in Java

Packages are folders / namespaces that organise related classes. First line of the file: package com.shop.order; The folder path must match that statement.

import com.shop.order.Order; brings a class from another package. Packages avoid name clashes (two Order classes in different packages) and keep large projects tidy. Default access members are visible inside the same package.

Wrong folder vs wrong package statement is a compile headache. Interview line: package = namespace + organisation; import brings classes in.

Let's take this on the board with one tiny Main class — no extra files. Packages in Java — secret is private; outside code uses a getter. Start from main, go line by line, and stop at each print. That output is the proof for Packages.

Exam tip

package statement + matching folder structure.

Example

// Same-file demo of access idea
class Box {
  private int secret = 7;
  int getSecret() { return secret; } // package/public API
}
public class Main {
  public static void main(String[] args) {
    System.out.println(new Box().getSecret());
  }
}

Packages in Java — secret is private; outside code uses a getter.

Short notes

  • DefPackage = folder/namespace for related classes.
  • Rulepackage statement at top. Folder path must match.
  • Useimport to use classes from another package.
  • RememberAvoids name clashes. Default access is same-package.

Questions

1

What is a package?

2

Where does the package line go?

3

What does import do?

Previous← Difference between Abstract Class and Interface in JavaNextAccess Modifiers in Java →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.