java - Have I used the this keyword correctly in my class? -


i'm new programming, 1 of our first object oriented programs doing in class. feel have used "this." more need to, program works , correct output. in getters, can return variable without using this? guess question this.variablename refer parameter variable or data field declared @ top of class?

import java.util.date;  public class account{      private int id = 0;     private double balance = 0;     private static double annualinterestrate = 0.00;     private date datecreated;      public account(){}      public account(int id, double balance){         this.id = id;         this.balance = balance;     }      public int getid(){         return this.id;     }      public void setid(int id){         this.id = id;     }      public double getbalance(){         return this.balance;     }      public void setbalance(double balance){         this.balance = balance;     }      public double getannualinterestrate(){         return this.annualinterestrate;     }      public void setannualinterestrate(double annualinterestrate){         this.annualinterestrate = annualinterestrate;     }      public date getdatecreated(){         return this.datecreated = new date();     }      public double getmonthlyinterestrate(){         return (this.annualinterestrate / 12);     }      public double getmonthlyinterest(){         return ((this.annualinterestrate / 100) / 12) * this.balance;     }      public void withdraw(double amount){         this.balance -= amount;     }      public void deposit(double amount){         this.balance += amount;     }  } 

here main method , test class:

public class accounttest{      public static void main(string[] args){          account myobject = new account(112233, 20000.00);         myobject.setannualinterestrate(4.5);         myobject.withdraw(2500.00);         myobject.deposit(3000.00);          system.out.printf("the account balance $%,.2f.", myobject.getbalance());         system.out.printf("\nthe monthly interest $%,.2f.", myobject.getmonthlyinterest());         system.out.print("\nthe account created @ " + myobject.getdatecreated());        }   } 

have used keyword correctly in class?

yes. not incorrect use this when strictly unnecessary. people argue makes intent more clear. either way, stylistic decision.

in getters, can return variable without using this?

yes.

i guess question this.variablename refer parameter variable or data field declared @ top of class?

this.name refers field not parameter.

name could refer either parameter or field. if there (in-scope) parameter , field same name, refer to parameter. hence if wrote:

public void setid(int id){     id = id; } 

you assigning value of parameter id itself. "fix" broken setid method either use this or change name of parameter; e.g. make newid.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -