new keyword in numberFormat class

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()
LVL 1
rgb192Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
CPColinConnect With a Mentor Senior Java ArchitectCommented:
NumberFormat is an abstract class, so you can't instantiate it using the new operator.
1
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
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".
1
 
Jan LouwerensConnect With a Mentor Software EngineerCommented:
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.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
@rgb192,

Do you have any further questions on this subject, or are you happy with the above comments?
0
 
rgb192Author Commented:
so it is static and what is class under it in the factory
0
 
Jan LouwerensSoftware EngineerCommented:
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).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.