{"id":1278,"date":"2017-05-06T05:50:00","date_gmt":"2017-05-06T05:50:00","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1278"},"modified":"2024-11-16T14:58:00","modified_gmt":"2024-11-16T14:58:00","slug":"how-to-generate-setter-and-getter-automatically-for-a-bean","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-generate-setter-and-getter-automatically-for-a-bean\/","title":{"rendered":"How to generate setter and getter automatically for a Bean?"},"content":{"rendered":"<h3>Overview<\/h3>\n<p>JavaBeans are POJO ( Plain Old Java Objects ) classes that encapsulate objects into a single entity called Bean. They are serializable, have a zero-argument constructor. The main property of the Bean class is, it allows access to its properties by using getter and setter methods. These methods are called accessor and mutator methods respectively.<\/p>\n<h3>Conventions<\/h3>\n<p>The class must have a public default no-arg constructor and properties must be accessible using get, set methods.<br \/>\nThe class should be serializable. This allows applications to store, and restore the bean\u2019s state.<\/p>\n<p>You can see that EmployeeBean has different properties and how to generate accessor methods automatically. The below tip saves a lot of time especially when you have more number of beans and each bean has lot of properties.<\/p>\n<h3>Eclipse Tip:<\/h3>\n<p>Once you write the variable names of the bean, go to<\/p>\n<p>Source &gt;&gt; Generate Setters and Getters<\/p>\n<p>Select the properties that you want setter\/getter methods and complete the wizard.<\/p>\n<p>The setter and getter code will be generated automatically.<\/p>\n<p>&nbsp;<\/p>\n<h4>EmployeeBean<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class EmployeeBean {\r\n\u00a0\u00a0 \u00a0String employeeName;\r\n\u00a0\u00a0 \u00a0int salary;\r\n\u00a0\u00a0 \u00a0String department;\r\n\u00a0\u00a0 \u00a0int bonus;\r\n\u00a0\u00a0 \u00a0String managerName;\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\/**\r\n\u00a0\u00a0 \u00a0 * @return the employeeName\r\n\u00a0\u00a0 \u00a0 *\/\r\n\u00a0\u00a0 \u00a0public String getEmployeeName() {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return employeeName;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\/**\r\n\u00a0\u00a0 \u00a0 * @param employeeName the employeeName to set\r\n\u00a0\u00a0 \u00a0 *\/\r\n\u00a0\u00a0 \u00a0public void setEmployeeName(String employeeName) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.employeeName = employeeName;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\/**\r\n\u00a0\u00a0 \u00a0 * @return the salary\r\n\u00a0\u00a0 \u00a0 *\/\r\n\u00a0\u00a0 \u00a0public int getSalary() {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return salary;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\/**\r\n\u00a0\u00a0 \u00a0 * @param salary the salary to set\r\n\u00a0\u00a0 \u00a0 *\/\r\n\u00a0\u00a0 \u00a0public void setSalary(int salary) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.salary = salary;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\/**\r\n\u00a0\u00a0 \u00a0 * @return the department\r\n\u00a0\u00a0 \u00a0 *\/\r\n\u00a0\u00a0 \u00a0public String getDepartment() {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return department;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\/**\r\n\u00a0\u00a0 \u00a0 * @param department the department to set\r\n\u00a0\u00a0 \u00a0 *\/\r\n\u00a0\u00a0 \u00a0public void setDepartment(String department) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.department = department;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\/**\r\n\u00a0\u00a0 \u00a0 * @return the bonus\r\n\u00a0\u00a0 \u00a0 *\/\r\n\u00a0\u00a0 \u00a0public int getBonus() {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return bonus;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\/**\r\n\u00a0\u00a0 \u00a0 * @param bonus the bonus to set\r\n\u00a0\u00a0 \u00a0 *\/\r\n\u00a0\u00a0 \u00a0public void setBonus(int bonus) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.bonus = bonus;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\/**\r\n\u00a0\u00a0 \u00a0 * @return the managerName\r\n\u00a0\u00a0 \u00a0 *\/\r\n\u00a0\u00a0 \u00a0public String getManagerName() {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return managerName;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\/**\r\n\u00a0\u00a0 \u00a0 * @param managerName the managerName to set\r\n\u00a0\u00a0 \u00a0 *\/\r\n\u00a0\u00a0 \u00a0public void setManagerName(String managerName) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.managerName = managerName;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<pre><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-495 size-full\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Setters_and_Getters_JavaBean.jpeg\" sizes=\"auto, (max-width: 1365px) 100vw, 1365px\" alt=\"Setters_and_Getters_JavaBean\" width=\"1365\" height=\"636\" title=\"\"><\/pre>\n<h3>Bean sample usage:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class BeanUsage {\r\n\r\n\u00a0\u00a0 \u00a0public static void main(String[] args) {\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ Setters\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0EmployeeBean eb = new EmployeeBean();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0eb.setEmployeeName(\"Surendra K\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0eb.setDepartment(\"Quality Assurance\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0eb.setSalary(10000);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0eb.setManagerName(\"Regan M\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0eb.setBonus(2400);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ Getters\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"*********Employee Record***************\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Employee Name:\" + eb.getEmployeeName());\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Manager Name:\" +\u00a0 eb.getManagerName());\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0int monthlySalary = eb.getSalary() + eb.getBonus()\/12 ;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Salary:\" + monthlySalary\u00a0\u00a0 );\r\n\u00a0\u00a0 \u00a0}\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1278\" data-postid=\"1278\" class=\"themify_builder_content themify_builder_content-1278 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Overview JavaBeans are POJO ( Plain Old Java Objects ) classes that encapsulate objects into a single entity called Bean. They are serializable, have a zero-argument constructor. The main property of the Bean class is, it allows access to its properties by using getter and setter methods. These methods are called accessor and mutator methods [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1278","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1278"}],"version-history":[{"count":5,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1278\/revisions"}],"predecessor-version":[{"id":19638,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1278\/revisions\/19638"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}