Skip to main content

Stack using Array and Class Example in Java

3 min read Updated May 29, 2026
Share:
On this page (9sections)

Introduction

Stack Using Array And Class is a classic Java console program that demonstrates the concept with complete source code and sample output. Classic data structures such as stack, queue and linked list implemented in Java.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Definition

A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.

Stack Using Array And Class Example Program

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Stack {

    private int top;
    private int item[];

    Stack(int size) {
        top = -1;
        item = new int[size];
    }

    void pushItem(int data) {
        if (top == item.length - 1) {
            System.out.println("Stack is Full");
        } else {
            item[++top] = data;
            System.out.println("Pushed Item :" + item[top]);
        }
    }

    int popItem() {
        if (top < 0) {
            System.out.println("Stack Underflow");
            return 0;
        } else {
            System.out.println("Pop Item : " + item[top]);
            return item[top--];
        }
    }
}

class StackExample {

    public static void main(String[] args) throws IOException {
        Stack stk = new Stack(5);
        boolean yes=true;
        int choice;
        BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
        
        do{
            System.out.println("1).Push\n2).Pop\n3).Exit\n\nEnter Choice");
            choice = Integer.parseInt(is.readLine());
            
            switch(choice)
            {
                case 1: System.out.println("Enter Push Item: ");
                        stk.pushItem(Integer.parseInt(is.readLine()));
                        break;
                case 2: stk.popItem();break;
                case 3: yes = false;break;
                default: System.out.println("Invalid Choice");
            }
        }while(yes==true);
        
    }
}

Sample Output

1).Push
2).Pop
3).Exit

Enter Choice
1
Enter Push Item: 
14
Pushed Item :14
1).Push
2).Pop
3).Exit

Enter Choice
1
Enter Push Item: 
567
Pushed Item :567
1).Push
2).Pop
3).Exit

Enter Choice
1
Enter Push Item: 
67
Pushed Item :67
1).Push
2).Pop
3).Exit

Enter Choice
1
Enter Push Item: 
789
Pushed Item :789
1).Push
2).Pop
3).Exit

Enter Choice
1
Enter Push Item: 
56
Pushed Item :56
1).Push
2).Pop
3).Exit

Enter Choice
1
Enter Push Item: 
99
Stack is Full
1).Push
2).Pop
3).Exit

Enter Choice
2
Pop Item : 56
1).Push
2).Pop
3).Exit

Enter Choice
2
Pop Item : 789
1).Push
2).Pop
3).Exit

Enter Choice
2
Pop Item : 67
1).Push
2).Pop
3).Exit

Enter Choice
2
Pop Item : 567
1).Push
2).Pop
3).Exit

Enter Choice
2
Pop Item : 14
1).Push
2).Pop
3).Exit

Enter Choice
2
Stack Underflow
1).Push
2).Pop
3).Exit

Enter Choice
3

When to use

Use this stack using array and class example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. import java.io.BufferedReader; imports a class used later in the program.

  3. import java.io.IOException; imports a class used later in the program.

  4. import java.io.InputStreamReader; imports a class used later in the program.

  5. top = -1; updates a variable used in the calculation or output.

  6. item = new int[size]; updates a variable used in the calculation or output.

  7. The if statement runs the nested code only when the condition is true.

  8. A println / print call writes text to the console — part of the sample output below.

Best Practices

  • Use meaningful variable and class names that describe their purpose.
  • Compile and run the program locally — modify values to see how output changes.
  • Read compiler errors carefully; they usually point to the exact line to fix.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Stack Using Array And Class program demonstrate?
It shows how to implement stack using array and class in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials