• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instantiating an object without new keyword and an expression with two dot notations?  RSS feed

 
Alan Ong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to all,

I'm learning Java from a book called Java How to Program 10th edition and I learnt that in order instantiate a class's object there must be a new keyword followed by the class.
For e.g.

However, there is this example regarding BigDecimal which does not utilize the keyword and yet it still creates an object of BigDecimal(line 11 - 12) which I don't understand why.
And also does this allocate a memory for the objects where normal objects get?

Additionally, at line 26 which codes:



The book explains that this expression uses the static method of NumberFormat's getCurrencyInstance to get a NumberFormat that's preconfigured to format numeric values as locale-specific currency strings and then invokes its format method on the object returned by getCurrencyInstance method to perform the formatting of the amount value.

I do kinda understand the explanation but have never seen this type of expression before. Can someone tell me what kind of expression it is and is there a term for it?
Please correct me if I'm wrong here. Am I correct to say that this means the results of the first method invoked will always be used by the second method invoked as a combination?

Thanks in advance!



 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like BigDecimal.valueOf(1000); is called a static factory method.  At some point in the code, it will return a new BigDecimal(), that is, when it all boils down to it, there is still a new keyword being used.

NumberFormat.getCurrencyInstance().format(amount)); is a little trickier, but NumberFormat.getCurrencyInstance() is also a static factory method, so it returns a new object.  You can then act on that new object by calling a method on it.  You've seen code like this, I'm sure:

Well, you can combine those two if you don't need to save the foo object:

That's what NumberFormat.getCurrencyInstance().format(amount)); is doing, just that it uses a factory method instead of new.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Ong wrote:
Can someone tell me what kind of expression it is and is there a term for it?
Please correct me if I'm wrong here. Am I correct to say that this means the results of the first method invoked will always be used by the second method invoked as a combination?

It's called method chaining and if I understand what you're trying to say, yes, your understanding appears to be correct.

To restate what's happening, first NumberFormat.getCurrencyInstance() is invoked. The method returns an object that has a format() method, which is then invoked by the ".format(amount)" part of the expression. The entire expression will result in whatever that .format(amount) method call results in.
 
Alan Ong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for taking the time to explain to me I will click on the link which you have provided to learn more about method chaining.

Junilu Lacar wrote:
Alan Ong wrote:
Can someone tell me what kind of expression it is and is there a term for it?
Please correct me if I'm wrong here. Am I correct to say that this means the results of the first method invoked will always be used by the second method invoked as a combination?

It's called method chaining and if I understand what you're trying to say, yes, your understanding appears to be correct.

To restate what's happening, first NumberFormat.getCurrencyInstance() is invoked. The method returns an object that has a format() method, which is then invoked by the ".format(amount)" part of the expression. The entire expression will result in whatever that .format(amount) method call results in.
 
Alan Ong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides learning that it is a static factory method I also learnt that we can either choose to save or not save an object thanks a million!

Knute Snortum wrote:Something like BigDecimal.valueOf(1000); is called a static factory method.  At some point in the code, it will return a new BigDecimal(), that is, when it all boils down to it, there is still a new keyword being used.

NumberFormat.getCurrencyInstance().format(amount)); is a little trickier, but NumberFormat.getCurrencyInstance() is also a static factory method, so it returns a new object.  You can then act on that new object by calling a method on it.  You've seen code like this, I'm sure:

Well, you can combine those two if you don't need to save the foo object:

That's what NumberFormat.getCurrencyInstance().format(amount)); is doing, just that it uses a factory method instead of new.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!