Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes object creation question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "object creation question" Watch "object creation question" New topic
Author

object creation question

amro talaat
Greenhorn

Joined: Sep 02, 2012
Posts: 4
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 ?!!!

Rob M Jones
Greenhorn

Joined: Nov 16, 2011
Posts: 2
Hi,
The GraphicsEnvironment.getLocalGraphicsEnvironment() method returns a GraphicsEnvironment object.
amro talaat
Greenhorn

Joined: Sep 02, 2012
Posts: 4
Rob M Jones wrote:Hi,
The GraphicsEnvironment.getLocalGraphicsEnvironment() method returns a GraphicsEnvironment object.


yeah thats make sense thanks
simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
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.
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

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

They are called factory methods.. Not Sometimes but in all cases..
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Aug 11, 2012
Posts: 375
    
    1

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Aug 11, 2012
Posts: 375
    
    1

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

Joined: Sep 02, 2012
Posts: 4
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 ???


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38511
    
  23
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

Joined: Aug 11, 2012
Posts: 375
    
    1

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

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4375
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object creation question