The Artima Developer Community
Sponsored Link

Java Buzz Forum
Constructors in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Constructors in java Posted: Mar 8, 2015 8:19 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Constructors in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Constructors will be executed when the object is created.
  • Constructors should have same name of class name.
  • Executed once per object.
  • Basically used to assign instance variables


  1. package instanceofjava;
  2. class A{
  3. A(){
  4. }
  5. }
  6. }

  • while creating object constructor will be executed so we can assign instance variables to some default values.

  1. package instanceofjava;
  2. class A{
  3. int a,b
  4. A(int x, int y){
  5. a=x;
  6. b=y;
  7. }
  8.  
  9. public static void main(String[] args){
  10.  A a=new A(10,20);

  11. }
  12. }

Types of constructors:

  • There are two types of constructors 
  • Default constructors
  • Parameterized constructor.

Default constructor:

  •  Default constructor will not have any arguments.
  • If we not defined any constructor in our class. JVM automatically defines a default constructor to our class.
  1. package instanceofjava;
  2. class Sample{
  3. int a,b
  4. Sample(){
  5. a=37;
  6. b=46;
  7. }
  8.  
  9. public static void main(String[] args){
  10.  
  11.  Sample obj=new Sample();
  12.   System.out.println(obj.a);
  13.   System.out.println(obj.b);
  14.  
  15. }
  16. }

Output:

  1. 37
  2. 46

Parameterized constructor


  1. package instanceofjava;
  2. class Sample{
  3. int a,b
  4. Sample(int x, int y){
  5. a=x;
  6. b=y;
  7. }
  8.  
  9. public static void main(String[] args){
  10.  
  11.  Sample obj=new Sample(37,46);
  12.   System.out.println(obj.a);
  13.   System.out.println(obj.b);
  14.  
  15. }
  16. }

Output:

  1. 37
  2. 46

Constructor overloading:

  1. package instanceofjava;
  2. class Sample{
  3. int a,b 
  4. Sample(){
  5. this(1,2);
  6.  System.out.println("Default constructor");
  7.  
  8. }
  9. Sample(int x , int y){
  10.  
  11. this(1,2,3); 
  12. a=x;
  13. b=y;
  14.  System.out.println("Two argument constructor");
  15.  
  16. }
  17.  
  18. Sample(int a , int b,int c){
  19.  System.out.println("Three argument constructor")
  20. }
  21. public static void main(String[] args){
  22.  
  23.  Sample obj=new Sample();
  24.   System.out.println(obj.a);
  25.   System.out.println(obj.b);
  26.  
  27. }
  28. }

Output:

  1. Three argument constructor
  2. Two argument constructor
  3. Default argument constructor
  4. 1
  5. 2





Read: Constructors in java

Topic: Agile Team Member Anti-Patterns Previous Topic   Next Topic Topic: Inheritance Definition and Programs

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use