{"id":637,"date":"2024-02-25T12:23:53","date_gmt":"2024-02-25T12:23:53","guid":{"rendered":"https:\/\/javigomez.org\/?p=637"},"modified":"2025-09-29T20:33:34","modified_gmt":"2025-09-29T20:33:34","slug":"u08-04-clases-en-java-constructores","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2024\/02\/25\/u08-04-clases-en-java-constructores\/","title":{"rendered":"U08-04-Clases en Java &#8211; Constructores"},"content":{"rendered":"\n<p>Los constructores en Java son m\u00e9todos especiales que se ejecutan al crear una nueva instancia de una clase. Se utilizan para inicializar los atributos del objeto y realizar otras tareas que sean necesarias al crear un nuevo objeto.<\/p>\n\n\n\n<p> Caracter\u00edsticas de los constructores: <\/p>\n\n\n\n<p>Tienen el mismo nombre que la clase. No tienen un tipo de retorno. Se pueden definir varios constructores con diferentes par\u00e1metros. Si no se define ning\u00fan constructor, Java crea un constructor por defecto sin par\u00e1metros.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package U2;\n\n\npublic class E00_00 {\n\n\tpublic static void main(String&#91;] args) {\n\t\t\n\t\tPersona p=new Persona(\"Javi\",56);\n\t\tSystem.out.println(p.nombre);\n\t\tSystem.out.println(p.edad);\n\t\t\n\t\tPersona p2=new Persona (\"Lara\");\n\t\tSystem.out.println(p2.nombre);\n\t\tSystem.out.println(p2.edad);\n\t\t\n\t\tPersona p3=new Persona();\n\t\tSystem.out.println(p3.nombre);\n\t\tSystem.out.println(p3.edad);\n\t\t\n\n\t}\n}\n\nclass Persona{\n\tString nombre;\n\tint edad;\n\t\n\tPersona (){\n\t\tthis.nombre=\"Indeterminado\";\n\t\tthis.edad=99;\n\t}\n\t\n\tPersona (String n){\n\t\tthis.nombre=n;\n\t\tthis.edad=10;\n\t}\n\tPersona (String n, int e){\n\t\tthis.nombre=n;\n\t\tthis.edad=e;\n\t}\n\t\n\tvoid cumple() {\n\t\tthis.edad++;\n\t}\n}\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package U2;\n\n\npublic class E00_00 {\n\n\tpublic static void main(String&#91;] args) {\n\t\t\n\t\tPersona p=new Persona(\"Javi\",56);\n\t\tSystem.out.println(p.nombre);\n\t\tSystem.out.println(p.edad);\n\t\t\n\t\tPersona p2=new Persona (\"Lara\");\n\t\tSystem.out.println(p2.nombre);\n\t\tSystem.out.println(p2.edad);\n\t\t\n\t\tPersona p3=new Persona();\n\t\tSystem.out.println(p3.nombre);\n\t\tSystem.out.println(p3.edad);\n\t\t\n\n\t}\n}\n\nclass Persona{\n\tString nombre;\n\tint edad;\n\t\n\tPersona (){\n\t\tthis(\"Indeterminado\",99);\n\t}\n\t\n\tPersona (String n){\n\t\tthis(n,10);\n\n\t}\n\tPersona (String n, int e){\n\t\tthis.nombre=n;\n\t\tthis.edad=e;\n\t}\n\t\n\tvoid cumple() {\n\t\tthis.edad++;\n\t}\n}\n\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Java U8 04 Clases Constructores\" width=\"1200\" height=\"900\" src=\"https:\/\/www.youtube.com\/embed\/q0gL-BUCNQw?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Los constructores en Java son m\u00e9todos especiales que se ejecutan al crear una nueva instancia de una clase. Se utilizan para inicializar los atributos del &hellip; <\/p>\n","protected":false},"author":1,"featured_media":638,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[79,92,203,265],"class_list":["post-637","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-clases","tag-constructores","tag-java","tag-poo"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/comments?post=637"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/637\/revisions"}],"predecessor-version":[{"id":891,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/637\/revisions\/891"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media\/638"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}