new keyword in numberFormat class

rgb192
rgb192 used Ask the Experts™
on
package com.example.java;

import java.text.NumberFormat;

public class Main {

    public static void main(String[] args) {
	int R = 42;
    String fromR = Integer.toString(R);
    System.out.println(R);

    boolean Rb = true;
        Boolean.toString(Rb);
        System.out.println(Rb);


        long Rl = 100_000;
        //Long.toString(Rl);
        NumberFormat formatt = NumberFormat.getNumberInstance();
        String formatted = formatt.format(Rl);
        //with commas instead of underscore
        System.out.println(formatted);

    }
}

Open in new window




numberformatting

is this an existing class

why do I not need a NEW keyword

numberFormat object=new numberFormat()
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Java Architect
Commented:
NumberFormat is an abstract class, so you can't instantiate it using the new operator.
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
And just to expand a little more on the above, the method "getNumberInstance" is a static method of the class. This means that it is a method of the class and not an object, and so you don't NEED an object to call that method. The "new" keyword creates an object of a class, but since you don't need the object, you don't need to use "new".
Jan LouwerensEngineering Manager
Commented:
getNumberInstance() is what's called a Factory Method. If you'd like to know more information about how these work, there is a ton of information about this design pattern on the web.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
@rgb192,

Do you have any further questions on this subject, or are you happy with the above comments?

Author

Commented:
so it is static and what is class under it in the factory
Jan LouwerensEngineering Manager

Commented:
The exact implementation class is unknown. It's only known that it implements the NumberFormat interface (which is what you should be programming to, anyway).

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial