← Back to Core Java Course | Chapter 14: Advanced Topics | Lesson 5 of 6

Java में Packages और Import

Packages folders हैं जो related Java classes को group करते हैं, और import आपको एक को अपनी file में लाने देता है, एक shelf से एक tool fetch करने जैसा। यह names को organized रखता है।
Syntax
java
package package.name;

import package.name.ClassName;
import package.name.*;

Standard Class Imports

एक standard class import (import java.util.ArrayList;) एक specific class को current file में इसके short name से available बनाता है, हर बार उपयोग होने पर इसका fully qualified path लिखने की ज़रूरत के बिना।

उदाहरण: Standard Class Imports

java
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<>(); // short name, no full path needed
		list.add("a");
		System.out.println(list);
	}
}

Wildcard Package Imports

एक wildcard import (import java.util.*;) एक साथ किसी package की हर public class लाता है, जो convenient है लेकिन exactly यह छुपा सकता है कि एक file असल में किन classes पर निर्भर करती है।

उदाहरण: Wildcard Package Imports

java
import java.util.*;
public class Main {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>(); // brings in every public class from java.util
		list.add("a");
		System.out.println(list);
	}
}

Static Imports

एक static import (import static java.lang.Math.PI;) आपको एक class का static member हर बार qualify करने (Math.PI) के बजाय सीधे नाम (PI) से reference करने देता है, जो अक्सर उपयोग होने वाले constants या methods के लिए handy है।

उदाहरण: Static Imports

java
import static java.lang.Math.PI;
public class Main {
	public static void main(String[] args) {
		System.out.println(PI); // no Math. prefix needed
	}
}

Using Fully Qualified Names

आप हमेशा import को पूरी तरह skip कर सकते हैं और एक class को इसके fully qualified name (java.util.List<String>) से reference कर सकते हैं, जो कभी-कभी अलग packages से same simple name वाली दो classes को disambiguate करने के लिए ज़रूरी है।

उदाहरण: Using Fully Qualified Names

java
public class Main {
	public static void main(String[] args) {
		java.util.List<String> list = new java.util.ArrayList<>(); // no import at all
		list.add("a");
		System.out.println(list);
	}
}

Implicitly Imported Packages

java.lang में classes (जैसे String, Object, और System) हर Java file में बिना किसी import statement के अपने आप available हैं, क्योंकि वह package हर जगह implicitly imported है।

उदाहरण: Implicitly Imported Packages

java
public class Main {
	public static void main(String[] args) {
		String s = "Hello"; // java.lang.String, no import needed
		System.out.println(s);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Import भूल जाना, इसलिए class नहीं मिलती।
  2. import java.util.*; उपयोग करना और दो classes के बीच एक name conflict होना, जैसे java.util.Date और java.sql.Date।
  3. यह सोचना कि wildcard imports subpackages शामिल करते हैं, जब java.util.* में java.util.concurrent शामिल नहीं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.