top of page

Packages and Encapsulation in Java

Are you new to Java and looking to refresh your knowledge of its core concepts? Look no further!

Check out this quick walkthrough on Object-oriented programming in Java application development.

It’s perfect for beginners who want to brush up on their skills. Let’s dive in!

To fully understand it, it's important to have a grasp on how OOPS operates. Essentially, it revolves around the usage of classes and objects.

Class is a bind of data and functions which is basically properties and their behavior.

  • Even though a class is declared, it does not occupy memory.

  • In a simple way, we can say, a person is a class that acts like a blueprint with any name of the person as an object. so it can be any number of objects.

Object interacts by invoking methods and by using a new keyword we create objects.

  • As soon as the object is created memory is allocated.

  • A method is used mainly for Code Reusability and Optimization.

Access modifiers define the scope of the field in the application.

  • Public-- accessible from everywhere in the program.

  • Private--only visible within the enclosing class.

  • Protected -- only visible within the enclosing class and any subclasses.

  • Default-- accessible only within the same package.

From Chaos to Order- Organizing Your Java Code with Packages

Every Java file was developed inside src. Let’s take a scenario. If 100 java files or somehow 1000 java files all were developed in src, what happens?

  •  If the file count increases automatically managing difficulty also gets increased.

  • It is not advisable to develop more files in the same folder.

  •  Identifying a file can create conflict so go for fewer number files as possible.

How can we overcome this?

  • Instead of creating all java files in src, we can go for multiple folders let’s say pack1 contains some java files, pack2 contains some java files, and pack3 contains some java files so on.

  • Based on the usage or type we can go for different folders.

How does the package work?

  • To group similar kinds of classes, we go for packages.

  • For every java file, we need to specify the package name as a folder name like package1, and should be in lowercase.

Package package1;
class A //The current directory for this java file, is package1.
  • There are 2 different types of packages which are Built-in and user-defined.

  • User-defined packages are developed by the user whereas Built-in are defined in java libraries.

  • The best practice is to create using our domain specifically to avoid naming conflicts.

  • is one package with class A and is another package with the same class A and is a nested package with the same class A.

For built-in type, we can have simple examples for better understanding.

Example 1

//import static java.lang.Math.sqrt; or
import static java.lang.Math.*;
 /*java.lang is built-in and static import after jdk 1.5 with all math class files*/
public class A {
    public static void main(String[] args) {
            System.out.println(sqrt(5));/* it is not required to create an object instead we use static keyword*/


Example 2


import java.lang.Math;

public class A {
        public static void main(String[] args) {
           // System.out.println(sqrt(6));
           /* throws error we need to add math class otherwise it will check with sqrt method in A class.*/


import java.util.Date;// built-in package or nested package

//or we can use import java.util.*; which includes all libraries in utilities package.

public class A {
    public static void main(String[] args){
        Date today=new Date();

Point to remember while Structuring Your Code

  • In 1 folder we shouldn’t create an file again and again.

  • what a package is and why we should use them? if we are using packages not only encapsulate similar kinds of members in one package, we can also achieve avoiding naming collisions.

  • package declaration must be the first statement, followed by the package import.

  • Java uses file system directories to store packages.

  • Java packages provide access control to the classes.

  • Packages in Java add another dimension to access specifiers which in turn represent data encapsulation or data hiding.

  • It improves efficiency and coding style but also reduces a lot of additional work.

Encapsulation Demystified- Ways to Build More Secure Code

Before jumping into one of the fundamental concepts of Object-oriented programming, we need to know static vs non-static members inside the class with a simple example.

  • Let's assume a person, in general, has 2 hands, even from person to person it is not changing which is almost the same which is static.

  • But the height, weight, color, and age will differ from person to person so it is non-static.

​A class can have any number of static members

A class can also have any number of non-static members

They are loading to the memory only one time for the entire execution

Only one time for one object creation. for every one object the non-static member loads to the memory.

common to every object is static

specific to objects is non-static. an object is also called an instance so it is an instance members

  • The entity in object-oriented language represents a class ie, Entity mapping to class.

  • One entity mapping to one class -> encapsulation which is binding code + data.

  • Designing a class for one particular entity is encapsulation which is encapsulating every piece of information of the entity.

  • Entity containing properties and behaviors.

  • class -> one unit-> entity information-> properties + behaviors+ initializers.

  • we need to know how properties and behaviors can be represented in a class unit.

  • Property of the entity mapping through the attribute of the class similarly, Behavior of the entity mapping through a method of a class.

  • Enables us to write reusable programs and acts like a black box to protect the data.

  • The public method acts as an interface for accessing private data even if the object of the class wants to access it.

  • It makes us invoke methods without any parameters as data and code are bound.


How do I use packages and encapsulation in a testing framework?
  • While implementing the POM framework, we can use encapsulation to maintain all locators in the POJO class as an object repository.

  • POJO stands for Plain Old Java Object.

  • Getter — is for read-only access and the setter — is for write-only access.

  • In the POJO class, we will declare all the variables as private and use getters to access the locators in the different classes with the page factory concept.

Hope it helps!

Happy Learning!! Never forget to explore !!

167 views0 comments

Recent Posts

See All


Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page