Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

new operator  RSS feed

 
Rami Najjar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
i just know that every time i try to create new object that i need to using new operator such as

StringBuilder sb = new StringBuilder();


but i wandering why i did not do the same here

LocalDate date1 = LocalDate.of(2015, Month.JANUARY, 20);
LocalDate date2 = LocalDate.of(2015, 1, 20);


i feel confused because i think i did not get the idea about creating object
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Rami!

LocalDate.of() is a method that internally uses the new keyword.
 
Rami Najjar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Stephan, you made my day
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an example of what is known as a Factory pattern; it hides the sometimes messy details of using the constructor and possibly other methods to create an instance variable. Another example of this is the creation of a Border for a JComponent - the BorderFactory.createXxxBorder methods.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!