Constructor is a special code block which is used to initialized all necessary states of class.
Main things that you should know about constructor:
Eclipse: Source>Generate Constructor using Fields
Idea: Code>Generate>Constructor
Example:
public class BankAccount {
String accountEmail; //variable
public BankAccount(){ //java constructor
}
Getters - set values to instance variables. Has void type and return nothing
Setters - retrieve values and expose variables. Has type of variable and return variable.
How to generate getters and setters:
Eclipse: Source>Generate Gettersa and Setters
Idea: Code>Generate>Getters and Setters
Example:
String accountEmail; //variable //getter
public String getAccountEmail() {
return accountEmail;
}
public void setAccountEmail(String accountEmail) { //setter
this.accountEmail = accountEmail;
}
Main things that you should know about constructor:
- It has to have the same name as class
- It doesn't have a return type.
- It is a first thing that runs when you initialize an object
- In Java every class has a constructor even if you don't specify it
Eclipse: Source>Generate Constructor using Fields
Idea: Code>Generate>Constructor
Example:
public class BankAccount {
String accountEmail; //variable
public BankAccount(){ //java constructor
}
Getters - set values to instance variables. Has void type and return nothing
Setters - retrieve values and expose variables. Has type of variable and return variable.
How to generate getters and setters:
Eclipse: Source>Generate Gettersa and Setters
Idea: Code>Generate>Getters and Setters
Example:
String accountEmail; //variable //getter
public String getAccountEmail() {
return accountEmail;
}
public void setAccountEmail(String accountEmail) { //setter
this.accountEmail = accountEmail;
}
Комментариев нет:
Отправить комментарий