Forums Register Login

object creation question

+Pie Number of slices to send: Send
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 ?!!!

1
+Pie Number of slices to send: Send
Hi,
The GraphicsEnvironment.getLocalGraphicsEnvironment() method returns a GraphicsEnvironment object.
+Pie Number of slices to send: Send
 

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



yeah thats make sense thanks
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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


They are called factory methods.. Not Sometimes but in all cases..
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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?)
+Pie Number of slices to send: Send
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 ???


+Pie Number of slices to send: Send
I cannot imagine writing a factory method and not making it static. For more discussion look here amd here, and find the book mentioned.
+Pie Number of slices to send: Send
 

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

+Pie Number of slices to send: Send
 

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.
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 984 times.
Similar Threads
Dynamic Visualising Data in a Table with Separate Remove Buttons
Jeanne Boyarsky - the early years ?
What is stored on Stack and Heap
Reason for Agile development (video)
Do the Java Specs Allow a Quotient that is off in the least significant Digit?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:41:38.