Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: instantiating static methods

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default instantiating static methods

    An example in Java for dummies:

    "The Java API has a class named NumberFormat, and the NumberFormat class has a static method named getCurrencyInstance. When you call NumberFormat.getCurrencyInstance() with nothing inside the parentheses, you get an object that can mold numbers into U.S. currency amounts. Listing 18-7 has an example."
    "The currency object has a format method. So to get the appropriate bunch of characters into the niceTotal variable, you call the currency objectís format method. You apply this format method to the variable total."

    I'm wondering why this class instantiation involves a method.
    Why is it NumberFormat currency =
    NumberFormat.getCurrencyInstance();

    instead of NumberFormat currency = new Numberformat(); ?

    import java.text.NumberFormat;
    import java.util.Scanner;
     
    class BetterProcessData {
     
        public static void main(String args[]) {
            Scanner myScanner = new Scanner(System.in);
            double amount;
            boolean taxable;
            double total;
            NumberFormat currency = 
                NumberFormat.getCurrencyInstance();
            String niceTotal;
     
            System.out.print("Amount: ");
            amount = myScanner.nextDouble();
            System.out.print("Taxable? (true/false) ");
            taxable = myScanner.nextBoolean();
     
            if (taxable) {
                total = amount * 1.05;
            } else {
                total = amount;
            }
     
            niceTotal = currency.format(total);
            System.out.print("Total: ");
            System.out.println(niceTotal);
        }
    }


  2. #2
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    48
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: instantiating static methods

    NumberFormat (Java Platform SE 7 )

    "If you are formatting multiple numbers, it is more efficient to get the format and use it multiple times so that the system doesn't have to fetch the information about the local language and country conventions multiple times."

    It's more efficient to retrieve your currency format and use it more than once.

Similar Threads

  1. Static methods and variables
    By beer-in-box in forum Java Theory & Questions
    Replies: 8
    Last Post: April 18th, 2013, 01:24 PM
  2. Static methods in java
    By paramjit.singh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2013, 07:11 AM
  3. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  4. When to use static methods?
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: June 7th, 2012, 01:51 AM
  5. Static Methods Problem
    By mysticCHEEZE in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2011, 09:09 AM