TheAlgorithms/Java · error · AssertionError

No instances.

Error message

No instances.

What it means

Thrown by the private constructor of PiApproximation to prevent instantiation. PiApproximation is a utility class whose members are static; creating an instance is never meaningful. The constructor is private and immediately throws AssertionError so reflection-based or accidental instantiation fails loudly.

Source

Thrown at src/main/java/com/thealgorithms/maths/PiApproximation.java:20

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Implementation to calculate an estimate of the number π (Pi).
 *
 * We take a random point P with coordinates (x, y) such that 0 ≤ x ≤ 1 and 0 ≤ y ≤ 1.
 * If x² + y² ≤ 1, then the point is inside the quarter disk of radius 1,
 * else the point is outside. We know that the probability of the point being
 * inside the quarter disk is equal to π/4.
 *
 *
 * @author [Yash Rajput](https://github.com/the-yash-rajput)
 */
public final class PiApproximation {

    private PiApproximation() {
        throw new AssertionError("No instances.");
    }

    /**
     * Structure representing a point with coordinates (x, y)
     * where 0 ≤ x ≤ 1 and 0 ≤ y ≤ 1.
     */
    static class Point {
        double x;
        double y;

        Point(double x, double y) {
            this.x = x;
            this.y = y;
        }
    }

    /**
     * This function uses the points in a given list (drawn at random)

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Do not instantiate PiApproximation; call its static methods directly (e.g., PiApproximation.someStaticMethod()).
  2. If a framework requires instantiable types, annotate/skip utility classes or wrap the static API behind an instance-based adapter you control.
  3. For reflection-heavy frameworks, register a custom factory or exclude the class from auto-instantiation.

Example fix

// before (reflection)
PiApproximation p = PiApproximation.class.getDeclaredConstructor().setAccessible(true) ? ... : null;

// after — call static API directly
double pi = PiApproximation.estimatePi(iterations);
Defensive patterns

Strategy: validation

Validate before calling

// never instantiate; call static methods directly
double pi = PiApproximation.someStaticMethod();

Prevention

When it happens

Trigger: Any attempt to construct a PiApproximation instance — via reflection (PiApproximation.class.getDeclaredConstructor().setAccessible(true).newInstance()) or by subclassing/inner-class access. Normal new PiApproximation() is blocked at compile time by the private constructor.

Common situations: Reflection-driven frameworks (some DI containers, serializers, ORMs) that try to instantiate classes via no-arg constructors; a copy-paste of a utility class into a context expecting instances; test code using reflection to construct objects.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/2237483d0b64e0d4. Report an issue: GitHub.