Java constructor overloading

In java constructors can be overloaded which means a Java class Contains more than one constructor with different arguments.
Example
package com.java2study;

public class Account {
private int accountId;
private String name;
private String address;

public Account() {
this.accountId = 101;
this.name = "aaaa";
this.address = "xxx";
System.out.println("name=" + name + "accountId=" + accountId
+ "address=" + address);

}

public Account(int accountId, String name, String address) {
super();
this.accountId = accountId;
this.name = name;
this.address = address;
System.out.println("name=" + name + "accountId=" + accountId
+ "address=" + address);
}

public static void main(String[] args) {
Account account = new Account();

Account account1 = new Account(102, "BBBB", "yyyy");

}

}

output

name=aaaa accountId=101address=xxx
name=BBBB accountId=102address=yyyy

No comments:

Post a Comment

-->