{"id":5274,"date":"2021-03-23T16:38:35","date_gmt":"2021-03-23T16:38:35","guid":{"rendered":"https:\/\/ded9.com\/?p=5274"},"modified":"2025-12-13T13:08:29","modified_gmt":"2025-12-13T13:08:29","slug":"complete-tutorial-for-builders-in-java-in-simple-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/complete-tutorial-for-builders-in-java-in-simple-language\/","title":{"rendered":"Complete Tutorial for Builders in Java Explained Simply"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">In this tutorial, you will get acquainted with builders in <a href=\"https:\/\/ded9.com\/what-is-object-orientation-in-java-and-how-to-use-it\/\">Java <\/a>and learn how\u00a0to create and use builders with the help of examples.<\/span><span id=\"more-50127\"><\/span><\/p>\n<h2><span style=\"font-size: 18pt;\">What is a Builder?<\/span><\/h2>\n<h1><span style=\"font-size: 18pt;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-257963 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/DesignPatternBuilderJavaExampleClassDiagram.png\" alt=\"What is a Builder?\" width=\"717\" height=\"380\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/DesignPatternBuilderJavaExampleClassDiagram.png 717w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/DesignPatternBuilderJavaExampleClassDiagram-300x159.png 300w\" sizes=\"(max-width: 717px) 100vw, 717px\" \/><\/span><\/h1>\n<p>The constructor is similar to the method (but not the technique) that is called immediately when the object is created.<\/p>\n<p>The Java compiler distinguishes between a method and a constructor by their names and return types. In Java, the constructor has a class-like name and returns no values.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">class Test {\r\n\r\nTest () {\r\n\r\n\/\/ constructor body\r\n\r\n}\r\n\r\n}<\/pre>\n<\/div>\n<p>Here, Test () is constructive. It has the same class name and no return type.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">class Test {\r\n\r\nvoid Test () {\r\n\r\n\/\/ method body\r\n\r\n}\r\n\r\n}<\/pre>\n<\/div>\n<p>Here, Test () has the same name as the class.\u00a0However, its return type is void.\u00a0Hence, it is not constructive but methodical.<\/p>\n<p><strong>Example: Builder in Java<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class ConsMain {<\/li>\n<li dir=\"ltr\">private int x;<\/li>\n<li dir=\"ltr\">\/\/ constructor<\/li>\n<li dir=\"ltr\">private ConsMain () {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cConstructor Called\u201d);<\/li>\n<li dir=\"ltr\">x = 5;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">ConsMain obj = new ConsMain ();<\/li>\n<li dir=\"ltr\">System.out.println (\u201cValue of x =\u201d + obj.x);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">Constructor Called<\/p>\n<p dir=\"ltr\">Value of x = 5<\/p>\n<\/blockquote>\n<p>Here, the ConsMain () constructor is called when the obj object is created.<\/p>\n<p>A constructor may or may not accept an argument.<\/p>\n<h2>Builder without arguments<\/h2>\n<p>If a Java constructor accepts no parameters, it is a no-argument constructor. Its structure is as follows:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">accessModifier ClassName () {\r\n\r\n\/\/ constructor body\r\n\r\n}<\/pre>\n<\/div>\n<p><strong>Sample constructor without arguments<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class NoArgCtor {<\/li>\n<li dir=\"ltr\">int i;<\/li>\n<li dir=\"ltr\">\/\/ constructor with no parameter<\/li>\n<li dir=\"ltr\">private NoArgCtor () {<\/li>\n<li dir=\"ltr\">i = 5;<\/li>\n<li dir=\"ltr\">System.out.println (\u201cObject created and i =\u201d + i);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">NoArgCtor obj = new NoArgCtor ();<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">Object created and i = 5<\/p>\n<\/blockquote>\n<p>Here, the NoArgCtor () constructor does not accept any parameters.<\/p>\n<p><strong>Did you notice that the creator of NoArgCtor () is private?<\/strong><\/p>\n<p>This is because the object is made from within the same class.\u00a0Hence, it can access the manufacturer.<\/p>\n<p>However, if the object is created outside of class, you must make access to that constructor public.\u00a0For example:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Company {<\/li>\n<li dir=\"ltr\">String domainName;<\/li>\n<li dir=\"ltr\">\/\/ object is created in another class<\/li>\n<li dir=\"ltr\">public Company () {<\/li>\n<li dir=\"ltr\">domainName = \u201cprogramiz.com\u201d;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public class CompanyImplementation {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Company companyObj = new Company ();<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;Domain name =&#8221; + companyObj.domainName);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">Domain name = programiz.com<\/p>\n<\/blockquote>\n<h2><span style=\"font-size: 18pt;\">Default builder<\/span><\/h2>\n<p>The Java compiler automatically creates an argument-free constructor at runtime if you do not make one. This builder is the default. The default constructor initializes each informal sample variable.<\/p>\n<p><img decoding=\"async\" class=\"lazyloaded aligncenter wp-image-50129\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/word-image-1.png\" alt=\"The Java compiler automatically creates an argument-free constructor at runtime if you do not make one. This builder is the default. The default constructor initializes each informal sample variable.\" width=\"228\" height=\"471\" data-ll-status=\"loaded\" \/><\/p>\n<p><strong>Example: Default builder<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class DefaultConstructor {<\/li>\n<li dir=\"ltr\">int a;<\/li>\n<li dir=\"ltr\">boolean b;<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">DefaultConstructor obj = new DefaultConstructor ();<\/li>\n<li dir=\"ltr\">System.out.println (\u201ca =\u201d + obj.a);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cb =\u201d + obj.b);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>The above program is equivalent to:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class DefaultConstructor {<\/li>\n<li dir=\"ltr\">int a;<\/li>\n<li dir=\"ltr\">boolean b;<\/li>\n<li dir=\"ltr\">private DefaultConstructor () {<\/li>\n<li dir=\"ltr\">a = 0;<\/li>\n<li dir=\"ltr\">b = false;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">DefaultConstructor obj = new DefaultConstructor ();<\/li>\n<li dir=\"ltr\">System.out.println (\u201ca =\u201d + obj.a);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cb =\u201d + obj.b);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">a = 0<\/p>\n<p dir=\"ltr\">b = false<\/p>\n<\/blockquote>\n<h2>Parametric constructor<\/h2>\n<p>A constructor may also accept parameters. Its structure is as follows:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">accessModifier ClassName (arg1, arg2,\u2026, argn) {\r\n\r\n\/\/ constructor body\r\n\r\n}<\/pre>\n<\/div>\n<p><strong>Example: Parametric constructor<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Vehicle {<\/li>\n<li dir=\"ltr\">int wheels;<\/li>\n<li dir=\"ltr\">private Vehicle (int wheels) {<\/li>\n<li dir=\"ltr\">wheels = wheels;<\/li>\n<li dir=\"ltr\">System.out.println (wheels + \u201dwheeler vehicle created.\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Vehicle v1 = new Vehicle (2);<\/li>\n<li dir=\"ltr\">Vehicle v2 = new Vehicle (3);<\/li>\n<li dir=\"ltr\">Vehicle v3 = new Vehicle (4);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">2 wheeler vehicle created.<\/p>\n<p dir=\"ltr\">3 wheeler vehicle created.<\/p>\n<p dir=\"ltr\">4 wheeler vehicle created.<\/p>\n<\/blockquote>\n<p>We passed an int (number of wheels) argument to the constructor when creating the object.<\/p>\n<h2><a href=\"https:\/\/en.wikipedia.org\/wiki\/Function_overloading\" target=\"_blank\" rel=\"noopener\">Overload<\/a> builders in Java<\/h2>\n<p>As with method overloading, you can have two or more manufacturers with the same name and parameters. For example:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Company {<\/li>\n<li dir=\"ltr\">String domainName;<\/li>\n<li dir=\"ltr\">public Company () {<\/li>\n<li dir=\"ltr\">this.domainName = \u201cdefault\u201d;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public Company (String domainName) {<\/li>\n<li dir=\"ltr\">this.domainName = domainName;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public void getName () {<\/li>\n<li dir=\"ltr\">System.out.println (this.domainName);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Company defaultObj = new Company ();<\/li>\n<li dir=\"ltr\">Company programizObj = new Company (\u201cprogramiz.com\u201d);<\/li>\n<li dir=\"ltr\">defaultObj.getName ();<\/li>\n<li dir=\"ltr\">programizObj.getName ();<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">default<\/p>\n<p dir=\"ltr\">programiz.com<\/p>\n<\/blockquote>\n<h2>important points<\/h2>\n<ul>\n<li>Immediately after calling the objects, the creators are called.<\/li>\n<li>The two laws of creation are:<\/li>\n<li>The Java constructor name must match the class name exactly.<\/li>\n<li>A Java constructor should not have a recursive type.<\/li>\n<li>If a class does not have a constructor, the Java compiler automatically generates a default constructor at runtime. The default constructor initializes the sample variables with the default values. For example, the variable int is equal to 0.<\/li>\n<li>Manufacturer types:<\/li>\n<li>No-Arg Constructor &#8211; A constructor that accepts no arguments<\/li>\n<li>Default Builder &#8211; A builder created automatically by a Java compiler.\u00a0(If not explicitly defined)<\/li>\n<li>Parametric constructor &#8211; Used to specify specific values \u200b\u200bof variables in an object<\/li>\n<li>Creators cannot be abstract, static, or final.<\/li>\n<li>The constructor can be defined several times.<\/li>\n<\/ul>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is the Builder pattern in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It is a design pattern used to construct complex objects step by step without using multiple constructors.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why should developers use builders in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Builders improve code readability, reduce constructor overload, and make object creation more flexible.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">When is the Builder pattern most useful?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It is useful when an object has many optional parameters or complex construction logic.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will get acquainted with builders in Java and learn how\u00a0to create and use builders with the help of examples. What is a Builder? The constructor is similar to the method (but not the technique) that is called immediately when the object is created. The Java compiler distinguishes between a method and [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":257966,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840],"class_list":["post-5274","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-java"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/comments?post=5274"}],"version-history":[{"count":6,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5274\/revisions"}],"predecessor-version":[{"id":266117,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/5274\/revisions\/266117"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/257966"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=5274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=5274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=5274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}