{"id":1433,"date":"2022-03-04T05:35:40","date_gmt":"2022-03-04T05:35:40","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1433"},"modified":"2022-03-04T05:35:40","modified_gmt":"2022-03-04T05:35:40","slug":"implement-private-constructors","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/04\/implement-private-constructors\/","title":{"rendered":"Implement private constructors"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">&nbsp;Java program to create a private constructor<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Test {\n\n  \/\/ create private constructor\n  private Test () {\n    System.out.println(\"This is a private constructor.\");\n  }\n\n  \/\/ create a public static method\n  public static void instanceMethod() {\n\n    \/\/ create an instance of Test class\n    Test obj = new Test();\n  }\n\n}\n\nclass Main {\n\n  public static void main(String&#91;] args) {\n\n    \/\/ call the instanceMethod()\n    Test.instanceMethod();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>This is a private constructor.<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a private constructor of the&nbsp;<code>Test<\/code>&nbsp;class. Hence, we cannot create an object of the&nbsp;<code>Test<\/code>&nbsp;class outside of the class.<\/p>\n\n\n\n<p>This is why we have created a&nbsp;<code>public static<\/code>&nbsp;method named&nbsp;<code>instanceMethod()<\/code>&nbsp;inside the class that is used to create an object of the&nbsp;<var>Test<\/var>&nbsp;class. And from the&nbsp;<code>Main<\/code>&nbsp;class, we call the method using the class name.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;Java program to create a private constructor Output In the above example, we have created a private constructor of the&nbsp;Test&nbsp;class. Hence, we cannot create an object of the&nbsp;Test&nbsp;class outside of the class. This is why we have created a&nbsp;public static&nbsp;method named&nbsp;instanceMethod()&nbsp;inside the class that is used to create an object of the&nbsp;Test&nbsp;class. And from the&nbsp;Main&nbsp;class, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[558],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1433"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1433"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1433\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}