Before implementing the Stack Class in Java, there is a need to understand how stack actually works in Java. The stack data structure in Java follows the Last In First Out order(LIFO). It works in such a manner that the element inserted at the end of the stack is removed from the beginning of the Stack. The Stack Class in Java implements the stack data structures.
In this write-up, we will implement the stack class in Java.
How to Use/Employ the Stack Class in Java?
The Stack class implements the Java stacks. The Stack class is declared with the stack keyword. Certain operations are performed on the stack, and the two major operations in the stack are push through which the elements are added to the stack, and pop which removes the element. Apart from these major operations, the other operations are peek(), search(), and empty().
Example 1: Implementation of Stack Class in Java
The code example below depicts how a stack is created in Java.
//Import the required package
import java.util.Stack;
class stackclass
{
public static void main(String[] args)
{
//Initializing the stack
Stack<Integer> s= new Stack<>();
//The result is returned either as true or false
boolean output = s.empty();
System.out.println("Is the declared stack empty? " + output);
// Adding the elements to the stack
s.push(88);
s.push(83);
s.push(90);
System.out.println("The elements in the Stack are: " + s);
output = s.empty();
System.out.println("Are there any elements in the stack? " + output);
}
}
In the above code:
- The package is imported.
- The “stackclass” is declared as the class.
- The empty() function retrieves a boolean value accordingly(true or false).
- The push() method inserts the elements into the stack.
- The elements in the stack are printed using the println() method.
Output
The below output shows that the stack is initially empty which is why a false is returned. When elements are pushed to the stack, the stack is printed and the boolean value changes to “true”.

Basic Operations on a Stack
The basic operations on the stack are depicted below:
- Pushing Elements
- Popping Elements
- Accessing Elements
Operation 1: Pushing Elements
The elements are added to the stack through the push() method which is depicted below.
//Import the required packaged
import java.io.*;
import java.util.*;
//Create a stack class
class stackclass {
public static void main(String[] args)
{
//Initialization of stack
Stack s1 = new Stack();
Stack<String> s2 = new Stack<String>();
//Add elements to the stack using push()
s1.push("This");
s1.push("is");
s1.push("Stack");
s2.push("Class");
s2.push("in");
s2.push("Java");
//Print the elements in the stack using the println() method
System.out.println("The first stack is: " +s1);
System.out.println("The second stack is: "+s2);
}
}
In the above code:
- The required package is imported.
- A class is declared as “stackclass” for adding the elements to the stack.
- The elements are added using the push() method.
- The output of two stacks is printed using the println() method.
Output
The below output depicts the insertion of elements in the Stack.

Operation 2: Removing Elements
The topmost element of the stack is deleted using the pop() method. Below is the implementation of the pop() method in Java
//Import the required packaged
import java.io.*;
import java.util.*;
//Create a stack class
class stackclass {
public static void main(String[] args)
{
//Initialization of stack
Stack s1 = new Stack();
Stack<String> s2 = new Stack<String>();
//Add elements to the stack using push()
s1.push("This");
s1.push("is");
s1.push("Stack");
s2.push("Class");
s2.push("in");
s2.push("Java");
//Print the elements in the stack using the println() method
System.out.println("The first stack is: " +s1);
System.out.println("The popped element is " +s1.pop());
System.out.println("The second stack is: "+s2);
}
}
The above code is similar to the previous one except for the difference that the pop() method is used to remove the element from the top of the stack.
Output
The below output shows that the element “Stack” is popped from the declared stack because it was inserted at the end of the stack.

Operation 3: Accessing the Elements from the Stack
The below code shows the implementation of the peek() method that accesses the last entered element from the stack.
//Import the required packaged
import java.io.*;
import java.util.*;
//Create a stack class
class peekclass {
public static void main(String[] args)
{
//Initialization of stack
Stack s1 = new Stack();
Stack<String> s2 = new Stack<String>();
//Add elements to the stack using push()
s1.push("This");
s1.push("is");
s1.push("Stack");
s2.push("in");
s2.push("Java");
s2.push("8");
//Print the elements in the stack using the println() method
System.out.println("The first stack is: " +s1);
System.out.println("The accessed element is " +s1.peek());
System.out.println("The second stack is: "+s2);
System.out.println("The accesses element is " +s2.peek());
}
}
In the above code:
- The required package is imported.
- The class for the peek() method is declared.
- The push method adds the elements to the first and the second stack declared as s1 and s2.
- The next step involves using the peek() method to access the element from the stack.
Output
The below output shows the last entered element which is “Stack” from Stack 1 and “8” from Stack 2.

Operation 4: Search the Elements From the Stack
The search() method takes the element as input and uses the integer value to declare the position at which the element is present which is described in the below code.
//Import the required packaged
import java.io.*;
import java.util.*;
//Create a stack class
class stackclass {
public static void main(String[] args)
{
//Initialization of stack
Stack s1 = new Stack();
Stack<String> s2 = new Stack<String>();
//Add elements to the stack using push()
s1.push("This");
s1.push("is");
s1.push("Stack");
//Print the elements in the stack using the println() method
System.out.println("The first stack is: " +s1);
//Declare the integer value to find the index of the element in the stack
int index = s1.search("This");
System.out.println("The Index of This: " + index);
}
}
In the above code:
- The stack class is declared with the element added using the push() method.
- The integer as the index is declared with the search() method that finds the element and returns the position of the element.
- The println() method prints the results.
Output
The output below depicts that the element in the stack “This” is printed along with the position it is present in the stack.

Conclusion
The Stack is implemented using the Stack class in Java. The Stack class implements certain operations like push(), pop(), peek(), search(), and empty(). In this article, we have expressed the working of the Stack class in Java along with its operations.