# Utilty class

# Utility Class in Java – Complete Guide (From Scratch)

## Introduction

In Java, sometimes we need a class that only provides methods and does not represent any real-world object. These classes are called **Utility Classes**. They are used to perform common operations like calculations, string operations, array operations, etc.

For example, Java already provides utility classes like:

*   Math
    
*   Arrays
    
*   Collections
    
*   Files
    

These classes contain only static methods and we don’t create their objects.

* * *

## What is a Utility Class?

A **Utility class** is a class that contains only static methods and a private constructor so that its object cannot be created.

### Definition

> A utility class is a class that contains only static methods, has a private constructor, and is used to perform common reusable operations.

* * *

## Why Use Utility Class?

Utility classes are used when:

*   Methods are common for all objects
    
*   Methods do not depend on instance variables
    
*   We want to reuse code
    
*   We don’t want to create objects again and again
    

Example tasks:

*   Addition
    
*   Square
    
*   String reverse
    
*   Array sorting
    
*   Validation
    

* * *

## How to Create a Utility Class

There are 3 main rules:

1.  Make the class `final`
    
2.  Make the constructor `private`
    
3.  Make all methods `static`
    

* * *

## Example: Creating Our Own Utility Class

```java
final class MyUtility {

    // Private constructor to prevent object creation
    private MyUtility() {
        System.out.println("Utility class object not allowed");
    }

    // Static methods
    public static int add(int a, int b) {
        return a + b;
    }

    public static int square(int a) {
        return a * a;
    }

    public static boolean isEven(int a) {
        return a % 2 == 0;
    }
}
```

* * *

## How to Use Utility Class

```java
class Test {
    public static void main(String[] args) {
        System.out.println(MyUtility.add(10, 20));
        System.out.println(MyUtility.square(5));
        System.out.println(MyUtility.isEven(4));
    }
}
```

### Output:

```plaintext
30
25
true
```

Notice: We are not creating object of MyUtility.

Wrong way:

```java
MyUtility obj = new MyUtility(); // Not allowed
```

Correct way:

```java
MyUtility.add(10, 20);
```

* * *

## Real-Life Utility Classes in Java

| Utility Class | Work |
| --- | --- |
| Math | Mathematical operations |
| Arrays | Array operations |
| Collections | Collection operations |
| Files | File handling |
| Paths | File path operations |

Example:

```java
System.out.println(Math.max(10, 20));
System.out.println(Math.sqrt(25));
```

* * *

## Utility Class vs Normal Class

| Feature | Utility Class | Normal Class |
| --- | --- | --- |
| Object Creation | Not Allowed | Allowed |
| Methods | Static | Static + Non-static |
| Constructor | Private | Public |
| Purpose | Common methods | Object creation |

* * *

## When Should You Create a Utility Class?

Create a utility class when:

*   All methods are static
    
*   No instance variables
    
*   Methods are reusable
    
*   Object creation is not required
    

Examples:

*   Password validation
    
*   Email validation
    
*   Math operations
    
*   Conversion (km to meter, rupee to dollar)
    
*   Date formatting
    

* * *

## Interview Questions

**Q1: Why constructor is private in utility class?** To prevent object creation.

**Q2: Why methods are static in utility class?** Because we don’t need object to call methods.

**Q3: Can we inherit utility class?** No, because it is final.

* * *

## Conclusion

Utility classes are very useful in Java for writing reusable code. They improve code reusability, performance, and structure. The most important point is that utility classes are never instantiated and are accessed using the class name.

**In short:**

> Utility class = final class + private constructor + static methods

* * *

## Small Practice Task

Create a utility class that has:

*   Method to find cube of number
    
*   Method to check prime number
    
*   Method to reverse a string
    

Try yourself — this is a very common interview/practical question.
