Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Packages In Java

  • A package in Java serves as a holding area for a group of classes, subpackages, and interfaces.
    • Packages are used to avoid name conflicts. For example, two packages may contain two classes named Student.
    • Packages are used to facilitate the search/location and use of classes, interfaces, enumerations, and annotations.
    • Packages are used to provide controlled access, such as protected and default access control. Classes in the same package and their subclasses can access a protected member. A default member (one that does not have an access specifier) is only accessible to classes in the same package.
    • Packages can be thought of as encapsulating data (or data-hiding).


Table Of Content

  • Packages in Java
  • Advantages of Packages
  • different way to access the package from outside the package
  • Using Static Import
  • Different ways to load the class files or jar files






  • The only thing left to do is group similar classes into packages. After that, we can use it in our program by writing a straightforward import class from pre-existing packages. A package is a container for a collection of related classes, some of which are kept for internal use while others are made accessible and exposed.As many times as we need to in our program, we can reuse already-existing classes from the packages.

  • Advantages of Java Package

    • To make classes and interfaces easier to maintain, they are categorized in Java packages.
    • Java package provide Access control
    • Fully qualified name




    Types of packages

    • Built-in Packages
    • User-defined packages

    Built-in Packages

    These packages include numerous classes that are a part of the Java API.

    • java.lang: includes language-supporting classes (e.g classed which defines primitive data types, math operations). The import of this package is automatic.
    • java.io: supports input and output operations with classes.
    • java.util: contains utility classes that implement date and time operations as well as linked lists, dictionaries, and other data structures.

    User-defined packages

    These are the packages the user has defined. First, we create the HELLO directory (name should be same as the name of the package). After that, create the Encapsulation inside the directory with the package names as the first statement.




    Example of Packages in Java

     // Java Packages  example  
    package HELLO;
    class Encapsulation{	 
        private String  EmpName; 
        private int  EmpId;
    	public String getEmpName() {
    		return EmpName;
    	}
    	public void setEmpName(String empName) {
    		EmpName = empName;
    	}
    	public int getEmpId() {
    		return EmpId;
    	}
    	public void setEmpId(int i) {
    		EmpId = i;
    	}   
    }
     
    class Main {
        public static void main(String[] args){
        	Encapsulation encapsulation = new Encapsulation();
        	encapsulation.setEmpName("Mohan");
        	encapsulation.setEmpId(421658);
            System.out.println("The Name of the Emp is : " +encapsulation.getEmpName()
                                                                            +" & EmpID is : "+encapsulation.getEmpId());
        }
    }
    

    The best way to run and compile a Java package without using an IDE


    Syntax

    To Compile: javac -d . Main.java

    To Run: java HELLO.Main


    The destination for the generated class file is specified by the -d switch. You are free to use any directory name, such as c:/example. Use if you want to maintain the package in the same directory (dot).




    Output:

    The Name of the Emp is : Mohan & EmpID is : 421658 


    3 way to access the package from outside the package

    • Import all classes and interfaces of package (import package.*;)
    • Import package declared class name (import package.classname;)
    • No Import, declared class of package


    Import all classes and interfaces of package (import package.*;)

    //PackageCheck.java


    package tutorial;
    public class PackageCheck{
       public void Print(){
          System.out.println("Hello World");
        }
    }
    

    //Main.java


    package HELLO;
    import tutorial.*;
    class Main {
        public static void main(String[] args){
        	PackageCheck packageCheck=new PackageCheck();
        	packageCheck.Print();
       }
    }
    



    Output:

    Hello World 


    Import package declared class name (import package.classname;)

    //PackageCheck.java


    package tutorial;
    public class PackageCheck{
       public void Print(){
          System.out.println("Hello World");
        }
    }
    



    //Main.java


    package HELLO;
    import tutorial.PackageCheck;
    class Main {
        public static void main(String[] args){
        	PackageCheck packageCheck=new PackageCheck();
        	packageCheck.Print();
       }
    }
    

    Output:

    Hello World 




    When you import a package, all of its classes and interfaces are imported except subpackages. So, you also need to import the subpackage.



    Fully qualified name


    Only this package's declared classes will be accessible if you use the fully qualified name. There is no longer a need to import. When utilizing a class or interface, however, you must always use the fully qualified name.When two packages belong to the same class, it is commonly used.

    //PackageCheck.java


    package tutorial;
    public class PackageCheck{
       public void Print(){
          System.out.println("Hello World");
        }
    }
    



    //Main.java


    package HELLO;
    class Main {
        public static void main(String[] args){
        	tutorial.PackageCheck packageCheck=new tutorial.PackageCheck();
        	packageCheck.Print();
       }
    }
    

    Output:

    Hello World 


    Using Static Import


    The Java programming language (versions 5 and above) introduced the concept of static import, which enables members (fields and methods) defined in a class as public static to be used in Java code without mentioning the class in which they are defined.

    //PackageCheck.java


    package HELLO;
    import static java.lang.System.*;
    class Main {
        public static void main(String[] args){
        	 out.println("DockerTpoint");
       }
    }
    



    Output:

    DockerTpoint 

    Different ways to load the class files or jar files

    • Temporary
      • By modifying the classpath in the command prompt
      • with the -classpath switch
    • Permanent
      • classpath can be set in environment variables
      • creating the class file jar file and placing it in the jre/lib/ext folder are both ways to install class files.



    Java Access Modifiers Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Encapsulation
Java Package
Java Access Modifiers
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.