1

Could anyone tell me what I am doing wrong right here?

private static java.lang.Math a = new java.lang.Math();

It's resulting an error, but I have no idea why.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
user2192658
  • 41
  • 1
  • 5

7 Answers7

9

You can't instantiate a Math instance. It's all static methods. There's no public default constructor.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 7
    There's no default _public_ constructor, but there is a _private_ one – Alex Apr 19 '13 at 17:25
  • 4
    And that's deliberate, to keep people from instantiating it. – Louis Wasserman Apr 19 '13 at 17:38
  • The odd thing is that `Math` really should not be a singleton class of static methods, since there are two classes with the same methods: `Math` and `StrictMath`. I can imagine more versions of the interface of `Math` that trade accuracy for speed. Look at the Quake 3 Inverse Square Root routine for reasons why you'd do this. – Eric Jablow Apr 19 '13 at 18:07
  • That's history; can't do anything about that. You'll see that the original Math was part of JDK 1.0, but StrictMath was not. – duffymo Apr 19 '13 at 18:21
  • Yes, I get it about the private constructor. – duffymo Apr 19 '13 at 18:22
3

Could anyone tell me what I am doing wrong right here?

The answer is you are trying to instantiate a class with a single private constructor, from outside the class.

/**
 * Don't let anyone instantiate this class.
 */
private Math() {}

a.abs(int min - int max); !!

You can do the following way :

int min = xxx;
int max = yyy;
int abs = Math.abs(min-max);
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
2

You're trying to instantiate java.lang.Math... and I'm not sure why.

Just use it like this:

java.lang.Math.sin(x);
Captain Skyhawk
  • 3,499
  • 2
  • 25
  • 39
1

Why to create new Object,Its final class and all methods are static.

I was trying to say why to create an object if all methods are static and since it's final ,constructor call may fail

Chandu
  • 67
  • 5
1

You can't (without reflection) create object of class if you don't have access to its constructor, like in this case, where constructor is private.

I already have a Math class so thats why I am trying other options

All important methods of java.lang.Math are static so you don't need instance of that class to invoke them. Just use Math.abs(x - y) or in your case java.lang.Math.abs(x - y) to specify that you want to use java.lang.Math, not your class.


In case you just want to use just few methods from java.lang.Math you can just import them like

import static java.lang.Math.abs;

and use it like

abs(10 - 20)

You can also try this way

java.lang.Math a = null;
a.abs(x - y);   // compilator will change it to 
                //java.lang.Math.abs(x - y) since abs is static method

but I would stay with java.lang.Math.abs(x - y) since it is more readable.

Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
1

You can not create an object of Math class because the constructor of Math class has a private modifier.

Further, all the methods in the class are static. So you can use them directly ( ex : Math.method() ). By extension, the presence of only static methods in the class does not ever necessitate object construction.

So, one cannot instantiate Math because it was designed such that no one should ever find a need to instantiate it.

Sarath Chandra
  • 1,850
  • 19
  • 40
Ahsan
  • 29
  • 1
1

You can instantiate Math, but you shouldn't, unless it is for some obscure reason.

public static void main(String[] args) throws Exception {
    Constructor<?> constructor = Math.class.getDeclaredConstructors()[0];
    constructor.setAccessible(true);
    Math math = (Math) constructor.newInstance();
    ...
}
cahen
  • 15,807
  • 13
  • 47
  • 78