{"id":78993,"date":"2020-06-08T09:00:31","date_gmt":"2020-06-08T03:30:31","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=78993"},"modified":"2020-06-08T09:00:31","modified_gmt":"2020-06-08T03:30:31","slug":"java-pojo-class","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/","title":{"rendered":"Java POJO class &#8211; Plain Old Java Object"},"content":{"rendered":"<p>Till now, we have discussed and used the normal objects in Java. In this article, we will learn a special type of object POJO.<\/p>\n<p><strong>POJO<\/strong> stands for <strong>Plain Old Java Object<\/strong>, which is used to increase the reusability and readability of the Java program.<\/p>\n<p>We will discuss how to use Java POJO Class and why it is important to learn it.<\/p>\n<h3>What is POJO class in Java?<\/h3>\n<p><strong>POJO<\/strong> stands for Plain Old Java Object which is a normal object that is not bound with special restrictions. They are only bound with some restrictions on Java Language Specifications.<\/p>\n<p>Simply, POJO acts as a pure data structure that has getter and setter methods. POJO objects do not require any classpath.<\/p>\n<p>POJO is the most widely used class in Java because it is very easy to write and understand. Sun Microsystems introduced the POJO class in Java in<strong> EJB 3.0.<\/strong><\/p>\n<p>POJO class can override certain methods such as Serializable or also from the Object class.<\/p>\n<p>A POJO must not do the following:<\/p>\n<p><strong>1.<\/strong> A POJO class must not extend the <strong>predefined classes<\/strong> such as HttpServlet, Arrays, Calendar, etc.<\/p>\n<p>For example, if we write, public class MyClass extends javax.servlet.http.HttpServlet, then the class MyClass can\u2019t be considered as POJO class.<\/p>\n<p><strong>2.<\/strong> A POJO class should not contain <strong>pre-specified annotations<\/strong>. For example, @Retention(RetentionPolicy.RUNTIME) public class MyClass{..} is not a POJO class.<\/p>\n<p><strong>3.<\/strong> A POJO class can\u2019t implement <strong>predefined interfaces<\/strong>. For example public class Test implements javax.ejb.EntityBean { \u2026 } can\u2019t be considered as a POJO class.<\/p>\n<p>Now, let\u2019s implement POJO class in Java.<\/p>\n<h4>Example of POJO class<\/h4>\n<p>POJO class generally defines an entity. For example, if you want an Intern class then you can create a POJO as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/ Intern POJO class to represent entity Intern\npublic class Intern {\n  \/\/ default field\n  String name;\n\n  \/\/ public field\n  public String role;\n\n  \/\/ private field\n  private double salary;\n\n  \/\/arg-constructor to initialize fields\n  public Intern(String name, String role, double salary) {\n    this.name = name;\n    this.role = role;\n    this.salary = salary;\n  }\n\n  \/\/ getter method for name\n  public String getName() {\n    return name;\n  }\n\n  \/\/ getter method for role\n  public String getRole() {\n    return role;\n  }\n\n  \/\/ getter method for salary\n  public Double getSalary() {\n    return salary;\n  }\n}<\/pre>\n<p>The above example clearly shows a well-defined POJO class. In this example, you can see that the access modifiers of the fields are not restricted.<\/p>\n<p>They can have any access modifiers like public, private, default, or protected. You can also note that in POJO class there is no need to add any constructor.<\/p>\n<p>We can use the POJO class in any Java code. POJO class is not tied to the framework. But we can\u2019t implement this POJO class with any real conventions to change the state of the class.<\/p>\n<p>POJO behaves like an object that incorporates the business logic of the application. The following figure shows the working of a POJO class.<\/p>\n<p>From the image, you can clearly see that the controller interacts with the business logic. The business logic, in turn, interacts with the POJO class for getting access to the database.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/Java-Beans.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-79028\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/06\/Java-Beans.jpg\" alt=\"Java Beans\" width=\"712\" height=\"285\" \/><\/a><\/p>\n<h3>What are Java Beans?<\/h3>\n<p><a href=\"https:\/\/courses.cs.washington.edu\/courses\/cse341\/99wi\/java\/tutorial\/beans\/whatis\/index.html\">Java Beans<\/a> are a special type of POJOs. But there are some restrictions on Java beans to become POJO. These restrictions are as follows:<\/p>\n<p><strong>1.<\/strong> All JavaBeans can be POJOs but all POJO classes can not be Java Beans.<\/p>\n<p><strong>2.<\/strong> Java Beans should implement the Serializable interface.<\/p>\n<p><strong>3.<\/strong> All the fields of the Java Beans should be private to provide complete control over the fields.<\/p>\n<p><strong>4.<\/strong> All the fields should have either getter or setter methods or both of them.<\/p>\n<p><strong>5.<\/strong> There must be a no-arg constructor in a bean.<\/p>\n<p><strong>6.<\/strong> We can access the fields only by constructor or getter setters.<\/p>\n<p><strong>Code to understand JavaBeans<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.pojoclass;\nimport java.io. * ;\nclass Student implements java.io.Serializable {\n  private int id;\n  private String name;\n  public Student() {}\n  public void setId(int id) {\n    this.id = id;\n  }\n  public int getId() {\n    return id;\n  }\n  public void setName(String name) {\n    this.name = name;\n  }\n  public String getName() {\n    return name;\n  }\n}\npublic class Test {\n  public static void main(String args[]) {\n    Student s = new Student(); \/\/object is created\n    s.setName(\"Rita\"); \/\/setting value to the object\n    System.out.println(\u201cName of the student is: \u201d + s.getName());\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nName of the student is: Rita<\/p>\n<h3>Java POJO Class vs Java Bean<\/h3>\n<p>Now that we have discussed both POJO class and Java beans, let&#8217;s discuss the differences between them:<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>POJO<\/b><\/td>\n<td><b>JAVA BEAN<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">There is no restriction on the POJO class other than those forced by Java Specification language.<\/span><\/td>\n<td><span style=\"font-weight: 400\">Java Bean is a special POJO class that has some restrictions.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">POJO class does not provide much control over the fields.<\/span><\/td>\n<td><span style=\"font-weight: 400\">Java Bean provides complete control over the members.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">POJO class may or may not implement a Serializable interface.<\/span><\/td>\n<td><span style=\"font-weight: 400\">A Java Bean must implement a Serializable interface.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">We can directly access the fields by their names<\/span><\/td>\n<td><span style=\"font-weight: 400\">We can access the fields inside the Java Bean only by getters and setters methods.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">There can be any of the access modifiers for fields.<\/span><\/td>\n<td><span style=\"font-weight: 400\">Fields in Java Beans must be only private.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">There may or may not be a no-arg constructor in the POJO class.<\/span><\/td>\n<td><span style=\"font-weight: 400\">There must be a no-arg constructor in Java Beans.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">We use the POJO class when there is no need to put restrictions on the fields and there can be complete access over the fields.<\/span><\/td>\n<td><span style=\"font-weight: 400\">We use Java Beans when we do not want to give complete access to the entities.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Conclusion<\/h3>\n<p>We learned the POJO class in Java which enhances the readability of code. We also discussed real-life entities.<\/p>\n<p>The JavaBeans is almost similar to the Java POJO class but it also has some differences that we discussed in this article.<\/p>\n<p>We hope you might have understood the working and implementation of the POJO class in Java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Till now, we have discussed and used the normal objects in Java. In this article, we will learn a special type of object POJO. POJO stands for Plain Old Java Object, which is used&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":79027,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[2798,2799,2800,2801,2802],"class_list":["post-78993","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java-pojo-class","tag-plain-old-java-object","tag-plain-old-java-object-with-example","tag-pojo-class-in-java","tag-pojo-class-in-java-example"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java POJO class - Plain Old Java Object - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn What is Java POJO Class with Example, What are Java Beans with restrictions and Example, Comparison between Java POJO and Java Beans\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java POJO class - Plain Old Java Object - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn What is Java POJO Class with Example, What are Java Beans with restrictions and Example, Comparison between Java POJO and Java Beans\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-08T03:30:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/POJO-Class-in-Java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java POJO class - Plain Old Java Object - TechVidvan","description":"Learn What is Java POJO Class with Example, What are Java Beans with restrictions and Example, Comparison between Java POJO and Java Beans","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/","og_locale":"en_US","og_type":"article","og_title":"Java POJO class - Plain Old Java Object - TechVidvan","og_description":"Learn What is Java POJO Class with Example, What are Java Beans with restrictions and Example, Comparison between Java POJO and Java Beans","og_url":"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-06-08T03:30:31+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/POJO-Class-in-Java.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Java POJO class &#8211; Plain Old Java Object","datePublished":"2020-06-08T03:30:31+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/"},"wordCount":802,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/POJO-Class-in-Java.jpg","keywords":["Java POJO Class","Plain Old Java Object","Plain Old Java Object with Example","POJO Class in Java","POJO Class in Java example"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/","url":"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/","name":"Java POJO class - Plain Old Java Object - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/POJO-Class-in-Java.jpg","datePublished":"2020-06-08T03:30:31+00:00","description":"Learn What is Java POJO Class with Example, What are Java Beans with restrictions and Example, Comparison between Java POJO and Java Beans","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/POJO-Class-in-Java.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/POJO-Class-in-Java.jpg","width":802,"height":420,"caption":"Java Pojo class"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-pojo-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java POJO class &#8211; Plain Old Java Object"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78993","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=78993"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78993\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/79027"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=78993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=78993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=78993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}