When I try to get the private data member of Java, I am using getter and setter method. But some how it is not coming in the input. Below is my code. What mistake I am doing? Also please can you suggest improvisation of the code to standard.
package Constructors;
public class Constructor {
//Bank HDFC has few fields to be declared
public String customerName;
public int age;
private long adharNumber;
protected long accountNumber;
protected double bankBalance;
short birthDate;
String birthMonth;
int BirthYear;
public short branchCode;
public String IFSCCode;
public String branchLocation;
public Constructor(long accountNumber,int age)
{
this();//calling constructor should be the first statement
this.accountNumber=accountNumber;
this.age=age;
System.out.println("Details of "+accountNumber+" are as follow:");
}
public Constructor() {
this.customerName="Harish";
this.adharNumber=12456789;
this.bankBalance=1200.56;
this.birthDate=20;
this.birthMonth="OCT";
this.BirthYear=1994;
this.branchCode=20;
this.IFSCCode="HDFC000003";
this.branchLocation="Bengaluru";
}
public void getProtected()
{
System.out.println("bankBalance: "+this.bankBalance);
}
public void getPrivate(long adharNumber)
{
this.adharNumber=adharNumber;
getAdharNumber();
System.out.println("adharNumber :"+adharNumber);
}
public void getPublic()
{
System.out.println("customerName :"+customerName);
System.out.println("branchCode :"+branchCode);
System.out.println("IFSCCode :"+IFSCCode);
System.out.println("branchLocation :"+branchLocation);
}
public void getDefault()
{
System.out.println("birthDate :"+birthDate);
System.out.println("birthMonth :"+birthMonth);
System.out.println("BirthYear :"+birthMonth);
}
public long getAdharNumber()
{
return adharNumber;
}
}
public class ConstructorTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Constructor c=new Constructor(22334455,29);
c.getPublic();
c.getProtected();
c.getDefault();
c.getPrivate();
}
}
Expected o/p:
Details of 22334455 are as follow:
customerName :Harish
branchCode :20
IFSCCode :HDFC000003
branchLocation :Bengaluru
bankBalance: 1200.56
birthDate :20
birthMonth :OCT
BirthYear :OCT
adharNumber :12456789
Actual o/p:
Details of 22334455 are as follow:
customerName :Harish
branchCode :20
IFSCCode :HDFC000003
branchLocation :Bengaluru
bankBalance: 1200.56
birthDate :20
birthMonth :OCT
BirthYear :OCT
adharNumber :0
New contributor
Tanuja Naik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2