Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

new keyword in numberFormat class

Posted on 2016-08-09
7
Medium Priority
?
39 Views
Last Modified: 2016-09-13
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()
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 800 total points (awarded by participants)
ID: 41749499
NumberFormat is an abstract class, so you can't instantiate it using the new operator.
1
 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 800 total points (awarded by participants)
ID: 41749670
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
 
LVL 5

Assisted Solution

by:Jan Louwerens
Jan Louwerens earned 400 total points (awarded by participants)
ID: 41753199
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 36

Expert Comment

by:mccarl
ID: 41771111
@rgb192,

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

Author Comment

by:rgb192
ID: 41792990
so it is static and what is class under it in the factory
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 41794490
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question