This

import java.lang.Math.*;

imports all (accessible) types declared within Math.

This

import java.lang.Math;

is redundant because Math is part of java.lang which is imported by default.

Both will require that you use

Math.PI

to access the field.

This

import static java.lang.Math.PI;

imports the static member Math.PI so that you can use its simple name in your source code.

Answer from Sotirios Delimanolis on Stack Overflow
🌐
O'Reilly
oreilly.com › library › view › javatm-how-to › 9780133813036 › ch06lev2sec5.html
Math Class static Constants PI and E - Java™ How To Program (Early Objects), Tenth Edition [Book]
February 24, 2014 - Math Class static Constants PI and E Class Math declares two constants, Math.PI and Math.E, that represent high-precision approximations to commonly used mathematical constants.... - Selection from Java™ How To Program (Early Objects), Tenth Edition [Book]
Authors   Paul DeitelHarvey Deitel
Published   2014
Pages   1248
🌐
Study.com
study.com › courses › business courses › business 104: information systems and computer applications
How to Use Pi Constant in Java - Lesson | Study.com
January 15, 2018 - To get the area, we need to multiply pi times the radius squared. First, we will calculate area, then the volume. To make the code easier to follow, we'll set the radius and length as constants, like this: import java.lang.Math.*; public class Pie { public static void main(String[] args) { //radius and length double radius = 5; double len = 15; // calculate the area using PI double area = radius * radius * Math.PI; //and now volume double volume = area * len; System.out.println("Volume of cylinder is: " + volume); } }
🌐
CodeGym
codegym.cc › java blog › java math › math.pi in java
Math.PI in Java
December 5, 2024 - “The ratio of a circle’s circumference to its diameter i-e; 22/7 is represented by a constant value of 3.14159 is called “pi” in mathematics.” What is Math.PI in Java? “Math.PI is a static final double constant in Java, equivalent ...
🌐
Scientech Easy
scientecheasy.com › home › blog › math class in java (with examples)
Math Class in Java (with Examples) - Scientech Easy
February 3, 2025 - PI is a frequently used Math class constant. Both constants defined in the Math class are public, static, final, and double. ... static, so only one copy will exist and we can access it using class name without constructing Math object. ... Let’s create a Java program to calculate the area ...
🌐
UCL
ee.ucl.ac.uk › mflanaga › java › Fmath.html
Michael Thomas Flanagan's Java Scientific Library: Maths functions and physical constants
public static final double EULER_CONSTANT_GAMMA = 0.5772156649015627; π (the ratio of the circumference of a circle to its diameter) Fmath.PI is included as a convenience and is equated to the java.lang.Math,
Find elsewhere
🌐
JavaBeat
javabeat.net › home › how to use pi in java?
How to Use PI in Java?
March 20, 2024 - It is a static constant that is called/used directly with the class name like this “Math.PI”. To make your code concise, import the “Math.PI” statically at the start of your program and use it anywhere without using the class name.
🌐
Reddit
reddit.com › r/learnprogramming › [java] using pi question
r/learnprogramming on Reddit: [Java] Using PI question
February 28, 2018 -

So I know I can do something like a direct import (not sure if that's the real name) but without importing java.lang.Math I can do a Math.PI and use this value for pi. I have an program where I use it more than once, so I think I should make a constant out of it. Personally I don't know why I can't put a Math.PI wherever I need it, because it seems clear and readable already, but I think for my assignment I need to adhere to this convention of use a number more than once with meaning > make constant.

How can I do that though? I have an import java.lang.Math; at the top of my program. Then in my class (but before my main) I have a public static final double PI = Math.PI;..so was there no need to import the Math package? How should I best do this?

🌐
Linux Hint
linuxhint.com › use-pi-in-java
How to Use Pi in Java – Linux Hint
It is also utilized in several ... programmers can utilize the “Math.PI” constant of the “java.lang” package. It is a static double type constant that belongs to the Java Math class:...
🌐
javaspring
javaspring.net › blog › pi-java
Mastering Pi in Java: A Comprehensive Guide — javaspring.net
import java.math.BigDecimal; import ... area); } } In Java, working with pi is straightforward thanks to the Math.PI constant provided by the Math class....
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › java › interpack › staticimport.html
The "Static Import" Construct
Some types define static constants or methods that are useful to other types. For example, the java.lang.Math class defines the PI constant and the cos method:
Top answer
1 of 16
401

That is perfectly acceptable, probably even the standard.

(public/private) static final TYPE NAME = VALUE;

where TYPE is the type, NAME is the name in all caps with underscores for spaces, and VALUE is the constant value;

I highly recommend NOT putting your constants in their own classes or interfaces.

As a side note: Variables that are declared final and are mutable can still be changed; however, the variable can never point at a different object.

For example:

public static final Point ORIGIN = new Point(0,0);

public static void main(String[] args){

    ORIGIN.x = 3;

}

That is legal and ORIGIN would then be a point at (3, 0).

2 of 16
234

I would highly advise against having a single constants class. It may seem a good idea at the time, but when developers refuse to document constants and the class grows to encompass upwards of 500 constants which are all not related to each other at all (being related to entirely different aspects of the application), this generally turns into the constants file being completely unreadable. Instead:

  • If you have access to Java 5+, use enums to define your specific constants for an application area. All parts of the application area should refer to enums, not constant values, for these constants. You may declare an enum similar to how you declare a class. Enums are perhaps the most (and, arguably, only) useful feature of Java 5+.
  • If you have constants that are only valid to a particular class or one of its subclasses, declare them as either protected or public and place them on the top class in the hierarchy. This way, the subclasses can access these constant values (and if other classes access them via public, the constants aren't only valid to a particular class...which means that the external classes using this constant may be too tightly coupled to the class containing the constant)
  • If you have an interface with behavior defined, but returned values or argument values should be particular, it is perfectly acceptible to define constants on that interface so that other implementors will have access to them. However, avoid creating an interface just to hold constants: it can become just as bad as a class created just to hold constants.
🌐
w3resource
w3resource.com › java-exercises › static_members › java-static-members-exercise-4.php
Java Program: Constants Class with Static Final Variable
This is appropriate since the method relies only on the static constant PI and does not require any instance-specific data. Main Method: The main method demonstrates how to use the static method calculateArea by calculating and printing the area of a circle with a given radius. This showcases the convenience of using static methods and variables for utility functions and constants. ... Write a Java program where the "Constants" class includes static final variables for common mathematical constants.
🌐
Wikipedia
en.wikipedia.org › wiki › Static_import
Static import - Wikipedia
October 5, 2023 - It also helps to deprecate the practice of creating a constant interface (an interface that only defines constants then writing a class implementing that interface, which is considered an inappropriate use of interfaces.) The mechanism can be used to reference individual members of a class: import static java.lang.Math.PI; import static java.lang.Math.pow;
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › math
Using Math Constants - Java Code Geeks
September 26, 2013 - Use Math.PI to get the double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter, ... package com.javacodegeeks.snippets.core; import java.lang.Math; public class MathConstants { public static void main(String args[]) { System.out.println("Pi = " + Math.PI); System.out.println("E = " + Math.E); } }
🌐
Oreate AI
oreateai.com › blog › understanding-constant-variables-in-java-a-deep-dive › 02c70170d275c99ffc8e917501114399
Understanding Constant Variables in Java: A Deep Dive - Oreate AI Blog
December 31, 2025 - class MathConstants { static final double PI = 3.14159; } Here, every object created from MathConstants will reference the same value for PI. and Usage Scenarios naming conventions enhance readability significantly when working with constants. It’s common practice to use uppercase letters with underscores separating words (e.g., MAX_VALUE).