• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

object creation question

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know that when you create an object you write something like this

Object object = new Object ()

but i found examples like this

Object object = Object.method() ;

what thats mean ???


if you dont understand what i want to say

i will make it more clear



https://www.youtube.com/watch?v=_Nt1KvRZ64A&feature=edu&list=PLA331A6709F40B79D

open this video and go to 3:48

i cant understand this line

GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment() ;

please explain this line ?!!!

 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The GraphicsEnvironment.getLocalGraphicsEnvironment() method returns a GraphicsEnvironment object.
 
amro talaat
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob M Jones wrote:Hi,
The GraphicsEnvironment.getLocalGraphicsEnvironment() method returns a GraphicsEnvironment object.



yeah thats make sense thanks
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that when you create an object using something like Object object = new Object() you are creating a variable with a name (on the left of the equals sign) and creating the actual object itself (on the right) by calling the constructor. When you do something like Object object = Object.method() you are doing the same thing except that the object being created (on the right) isn't created by calling a constructor but one is being returned for you as the result of the method. I think sometimes they are called factory methods.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

simon fletcher wrote:I think sometimes they are called factory methods.


They are called factory methods.. Not Sometimes but in all cases..
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, no, not every method which happens to return an object is a factory method. Consider for example an ordinary getter method (public Date getDate()) which gets a Date attribute from some object. You wouldn't call that a factory method, would you?

I think you would only call it a factory method if you knew that the method was actually creating the object it was going to return. Of course that's just a brief and incomplete description -- have a look at the Wikipedia article Factory method pattern for much, much, much more information about the pattern.
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Well, no, not every method which happens to return an object is a factory method. Consider for example an ordinary getter method (public Date getDate()) which gets a Date attribute from some object. You wouldn't call that a factory method, would you?

I think you would only call it a factory method if you knew that the method was actually creating the object it was going to return. Of course that's just a brief and incomplete description -- have a look at the Wikipedia article Factory method pattern for much, much, much more information about the pattern.



Well, the method he is showing is a static method. (It is called with class name)
And static method returning the instance of the class in which it is defined is called factory method (As far as I know)

So, public Date getDate() will be called on an object..

Now had this method been static and in Date class.. This would be factory method.. (That is the case here.. GraphicsEnvironment static method returning instance of that class will always be factory method))

Correct me if I am wrong in this.. :D
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, yes, it's true that a static method which returns an object is definitely more likely to be a factory method. I'd agree with that. But not 100% of them, I don't think.

Of course it's possible for a bad coder to write a bad application in which every method is static. Those methods aren't likely to be factory methods. But let's not pay any attention to those, let's stick to good code. I had a quick look through the Java API and came up with BigDecimal.valueOf(long), which returns a BigDecimal object. I don't think that's a factory method, at least not according to the Wikipedia article which I linked to. I'm sure there are lots of others, but the API doesn't use many static methods so it's time-consuming to review them.
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I had a quick look through the Java API and came up with BigDecimal.valueOf(long), which returns a BigDecimal object. I don't think that's a factory method, at least not according to the Wikipedia article which I linked to.


Yeah, that's not a factory method, and by reading that article, it got almost clear except one thing..
I came across a statement there: -
Factory methods encapsulate the creation of objects (The first line under the topic Encapsulation)
And I don't exactly understand the purpose of this statement as being used as an explanation of the concept..

I am saying this because, if you take a look at the java code given just above that topic.. (Complex class one..)
It has two methods, and isn't those methods following the above statement?? (So, exactly what is the difference between a Factory method and a static Factory method?)
 
amro talaat
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i havent read any book talking about factory methods
its the first time i know that something like this exist

why books dont talk about ???

and would i encounter many of factory methods in java ???

should i study it in depth ???


 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot imagine writing a factory method and not making it static. For more discussion look here amd here, and find the book mentioned.
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I cannot imagine writing a factory method and not making it static. For more discussion look here amd here, and find the book mentioned.



Sir you are right by saying that. But I was referring to the FactoryMethodPattern link that Paul gave me, and there I say this statement that says: -
"The code above is not a factory method pattern (we can not override static), but rather an example of static factory method"
This statement is given just below the first Java class (Complex class) on that link..

So, what I want to ask is, "Is that statement referring to Factory Method and Static Factory Method as two different concepts??"
I'll appreciate your response

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I cannot imagine writing a factory method and not making it static.


But that's the whole idea behind the Factory Method pattern. It allows you do swap in different factory implementations.
 
reply
    Bookmark Topic Watch Topic
  • New Topic