Solved

new keyword in numberFormat class

Posted on 2016-08-09
7
30 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
7 Comments
 
LVL 14

Accepted Solution

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

Assisted Solution

by:mccarl
mccarl earned 200 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 100 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 35

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 67
thymeleaf natural templating vs JSP 2 104
servlet doXXX methods 3 49
hibernate example for saving data 19 43
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

803 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