What is a POJO in Java? Almost EVERYONE Gets This Wrong

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what exactly is a pojo in java i've seen a lot of other youtube videos that claim to explain what a pojo is that are just wrong pojo sounds weird sounds fancy sounds complicated and it is absolutely none of those things you're gonna know exactly what it is and why there's some confusion around it by the end of this video my name's john and i put up a new java tutorial video every week so hit the subscribe button so you don't miss each week's video i also have a full java course available in the link down in the description if you're interested what is a pojo well you probably know that pojo just stands for plain old java object but what exactly does that mean what makes a class or an object a plain old java object here's exactly what it is a pojo is categorized not so much by what it has to have to be a pojo but what it has to not have there's three basic things it has to not extend any other class so it can't be a child class of another class it can't implement any interfaces and it can't use any fancy outside annotations what those three restrictions amount to it's just a simple class with no extra stuff that can be used by itself you don't need any outside libraries or other classes to use it and that's pretty much all the rules everything that you may have seen about needing like a public no args constructor and getters and setters those are all about something else that we're going to talk about later in this video but that's not the definition of a pojo so for example let's take a look at this cat class this cat class is dead simple public class cat and we have two properties int age and string name this is an example of a pojo it doesn't extend from another class it doesn't implement any interfaces and it doesn't have any fancy outside annotations so this class meets the definition of a pojo now if instead this cat extended an animal class then this would no longer fit the definition of a pojo because now i need this outside animal class in order to use it same thing goes for if it implemented an interface like um makes noise this doesn't fit the definition of a pojo either because i need this outside makes noise interface for it to work and as for the annotations part that's a little bit more complicated but for example there's this annotation called at entity and that's a special hibernate annotation which you need hibernate in order to use so if you had this entity annotation in your class it would no longer be a pojo now the term pojo actually came from this guy named martin fowler who was a programmer who wrote tons of books on software engineering he came up with the term pojo for one of his talks and he said we wondered why people were so against using regular objects in their systems and concluded that it was because simple objects lacked a fancy name so we gave them one and it's caught on very nicely basically people thought they were too cool to use regular objects in their programs so this guy came up with the name pojo in 2000 and everybody's been using it since now here's where i think a lot of people get mixed up they confuse a pojo with a java bean a pojo as we discussed is basically any class that can be used on its own but a java bean has some additional requirements it has to have a public no args constructor all of its properties or instance variables have to be private it has to have public getters and setters as needed and it has to be serializable i've seen a lot of people talk about needing a private new args constructor and getters and setters to be a pojo but that's not at all true it doesn't need that to be a pojo it does need those things to be a java bean for example this cat class here as we know is a pojo but it's not a java bean for a few reasons it does have the public no argus constructor even though it's not explicitly written here java will give it one by default but its instance variables its properties here age and name are not private and there's no getters and setters for them and this class is not serializable so even though it's a pojo it isn't a java bean for example here let's turn this cat pojo into a java bean so we already have the public no args constructed so how can we show that we already have a no args constructor available what exactly is it well we can go back to our main method you probably know that when you create an object you use the class name cat so we'll call it my cat equals new cat and do an open close parenthesis that's it that's a no args constructor it's just a certain kind of constructor that takes no parameters so whenever you say new whatever and you send no parameters that's you using a no args constructor and we didn't have to write that constructor ourselves in our cat class java gave it to us for free but right now our instance variables are not private and we need to make them private so private int age and private string name the next thing we need to be a java bean is to have getters and setters for these properties so we can just go ahead and use eclipse to create those for us we can just right click here go down to source and then generate getters and setters we can just check the getters and setters that we want and click generate and that's all we got to do eclipse has generated our getters and setters automatically now the last thing we need to be a java bean is for this class to be serializable to be serializable all we have to do is implement the serializable interface as a quick note in case you don't know what serializable means it's basically just a label for java that tells java that this class can be written to things like databases and files so now our pojo has become a java bean you'll hear a ton of people talk about pojos when they really mean java beans it's not a huge deal sometimes i even catch myself doing it but you can be happy that you now know the difference if you learned something in this video let me know with a like and be sure to hit the subscribe button so you don't miss each week's video thanks for watching i'll see you next time
Info
Channel: Coding with John
Views: 6,167
Rating: undefined out of 5
Keywords: java, codingwithjohn, coding with john, java beginner lesson, pojo, java pojo, what is a pojo class in java, pojo in java, what is a pojo, pojo java, pojo java example, java pojo class example, pojo class in java, java tutorial for beginners, java tutorial, learn java, pojo vs java beans, java beans vs pojo, pojo vs bean, pojo vs javabean
Id: oqPiEc2zNb0
Channel Id: undefined
Length: 5min 5sec (305 seconds)
Published: Sun May 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.