1

How come whenever you make an instance of a class in Java you use:

Class nameOfClass = new Class();

which creates an object then you can call its methods by adding a period after the object name. How come you dont need to use the new keyword or the parentheses when using BigInteger, and can make a new object this way:

BigInteger first = BigInteger.valueOf(1);

I have poured through the documentation here, as well as, many google searches to find out why this is so to no avail.

Jonathan Komar
  • 2,678
  • 4
  • 32
  • 43
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
  • You could look at the source code, and you'll see that it do the same thing as you describe for `Class` : `return new BigInteger(val);` http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/math/BigInteger.java#BigInteger.valueOf%28long%29 – user2336315 Dec 09 '13 at 22:10
  • Yes, it's quite easy -- create a static method that internally does the `new` operation. – Hot Licks Dec 09 '13 at 22:10
  • 1
    interesting! I remember going over the factory method back in college, will definitely need to open up the books again lol. With so many answers, which one should I accept ?? – Eddie Martinez Dec 09 '13 at 22:13
  • You most certainly cannot write 'Class nameOfClass = new Class();' – user207421 Dec 09 '13 at 22:13
  • @EJP to make a new object from a class ??? I think that's what you're supposed to do.. – Eddie Martinez Dec 09 '13 at 22:19
  • I think he means literally, creating a new `Class` wont work. – Ross Drew Dec 09 '13 at 22:32

5 Answers5

3

That's called a factory method. A static method that creates and returns a new object for you.

They're handy when you have loads of ways of creating objects but don't really want to overload the method signature too much (i.e. have loads of different constructors)

Ross Drew
  • 8,163
  • 2
  • 41
  • 53
3

This is a static factory method, which returns an instance of BigInteger.

public static BigInteger valueOf(long val) 
{
         if (val == 0)
             return ZERO;

         if (val > 0 && val <= MAX_CONSTANT)
             return posConst[(int) val];

         else if (val < 0 && val >= -MAX_CONSTANT)
             return negConst[(int) -val];

         return new BigInteger(val);
}

See, it's either returning a new BigInteger(val) or going through BigInteger array instance members to return an already existing BigInteger. For reference, here's the static block which creates the arrays:

private static BigInteger posConst[] = new BigInteger[MAX_CONSTANT+1];
private static BigInteger negConst[] = new BigInteger[MAX_CONSTANT+1]
static 
{
    for (int i = 1; i <= MAX_CONSTANT; i++) 
    {
        int[] magnitude = new int[1];
        magnitude[0] = i;

        posConst[i] = new BigInteger(magnitude,  1);
        negConst[i] = new BigInteger(magnitude, -1);
     }
}
Steve P.
  • 14,489
  • 8
  • 42
  • 72
1

That is a static factory method. A static method which is used to create and return a new object. So you can create a static method that internally calls new operation

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

This is because the BigInteger.valueOf method is a static factory method. That means that the method itself is simply used to create individual instances of BigInteger. This link gives a good description of when to use static methods: Java: when to use static methods

Community
  • 1
  • 1
drembert
  • 1,246
  • 1
  • 9
  • 18
1

BigInteger uses the factory method pattern to create new instances with more meaningful method names. The factory method also allows the JVM to reuse the same instance of commonly reusable values (such as 1) to save memory.

By the way, you CAN use the new keyword to construct new instances but the constructors take many parameters or Strings which may be confusing.

dkatzel
  • 31,188
  • 3
  • 63
  • 67