What are static factory methods in Java? - 036

preview_player
Показать описание
Imagine you're creating an class to represent coordinates. The coordinates could be two numbers representing the x and y axis, or the two numbers can represent a length and angle. Think about that for a second. This breaks because your constructor will only have two numbers for parameters, and no way to guess which one you mean. You could say integers for one, and floats for another but that's ugly and puts artificial constraints on your class. A constructor isn't going to work. We could use a builder pattern, but it seems overkill for two parameters. Lets look at a new pattern in Java called the static factory.

The static factory is a simple variation of the factory method pattern. The factory method pattern is a design pattern we'll cover later. The static factory method is simply a static method that returns an instance of a class. It might call a constructor to create an instance, or it might return an instance it already has created. Our next thought is, how does this help us?

#java #programming #tutorial





Concepts: Java, classes, java constructor chaining, constructors

Social Links: Don't hesitate to contact me if you have any further questions.

Related Videos:

Hide your class details with Java Access Modifiers - 027

Free Java Course Online

Media credits: All images are owned by DJ Spiess unless listed below:

Backgrounds

Music
Рекомендации по теме
Комментарии
Автор

I discovered this video while looking for materials to help me make sense of the principles in Joshua Bloch's "Effective Java". This first video seems to be helping so I will make sure to check out your other videos. Also, excellent production value! Thanks for making and posting these.

smolnyinstitute
Автор

your work is really great. Thank you so much for these tutorials. I find them so helpful.

DongBanGShinKIcassio
Автор

Thank you for the tutorials! :)
Your videos feel as if I watch a more colorful and engaging version of Joshua Bloch's "Effective Java"!
They are well organized, presented and articulated. It's really a pity that most teachers at colleges or universities do not have the charisma (or even put the effort) to actually convey knowledge or the excitement of the software engineering craft, but act as dull robotic speakerphones instead.

anime-grind
Автор

Hi. In the example at 3:38, DJ is talking about how you can prevent redundant instantiation, and highlighted the code to do this. But the if-block in the code checks if the specified index is null, and then returns the value at that index, which it has checked and found to be null. Shouldn't the block be if(coordArray[...] != null){...}? Or am I missing something?

kartikeysharma
Автор

Hello sir, can you please elaborate on returning an object of a subclass using static factory method ?

antarikshgoyal
Автор

Hi, Thanks for your videos. is there any order to watch the videos from beginning ?
if yes what is this order ?

alimolla
Автор

Hi there and thanks for this video. Just a short question to one slide you showed: When you use the if-statement in the factory method for a coordinate you check if this coordinate is already in the array of coordinates by doing ==null. But if this is the case, you return it, which makes no sense to me. Could this be a mistake? To me it would make perfect sense to say if there is none, create a new one, put it to the array and then return it. But as it seems, you return null, isnt it? So the line with the if-statement should say if !=null, return it, otherwise make a new one, save it and return it.

JanPhilippHeinrich
Автор

I saw many videos before this, your didatics it's awesome! really. My suggestion is to slow your explanation a little bit, talk more haltingly. Great video, congrats!

Oveia
Автор

I'm a begginer and I don't get the coordinate example...how does the coordArray know we already created an instance of that object if we do not add the newly instantied coordinate to that array before returning it?

rokomimica
Автор

Hi, I don't see the point on making the methods final? also the Intellij warns about removing the final from the method since already has "static". It cannot be overriden since it is static, so why the "final" ?

StefanBanu
Автор

At 3:38 you are returning null object - coordArray[y*10+x]

this makes more sense to me:

public static final Coordinate fromXY(int x, int y)
{

return coordArray[(y * 10) + x];

coordArray[(y*10)+x]=new Coordinate(x, y);
return coordArray[(y*10)+x];
}

pls do correct me if I am wrong...

amitrawat
Автор

Sir your explanation is good but fast.

wwehhh
Автор

According to Effective Java static factory methods have no relation to the GoF Factory pattern.

JayJay-kimi
Автор

The music is too loud and quite distracting 😶

ep
Автор

Play music instead of teaching....
It's like music video with background java tutorial 😅

virusehwag
Автор

do you have stutterness? the constant random pausing of speech is making me crazy

elfeLrOuNd