java - writing instance methods -


i'm practicing writing instance method converts natural number integer.

i following tutorial online , came code below. when compile, keeps adding "this" code compiles , gives me expected output i'm getting few warning messages. "this.i" correct syntax when creating instance method?

private naturalnumber i;  public int toint() {     int result = 0;     if (!(this.i.iszero())) {         int d = this.i.divideby10();         result = this.i.toint() * 10 + d;         this.i.multiplyby10(d);     }     return result; } 

whenever call internal fields or methods of object, keyword this, if not explicitly present, implied, referring object itself.

more details can found in java documentation, e.g. oracle:

so, this.i means "variable i in this object". of course,, i must have value assigned before invoking of methods this.i.iszero(), otherwise run-time error (nullpointerexception) occur. ide may warn against such error (e.g., eclipse underlines offending command jagged yellow line).

java ides can automatically add this keyword if specified in preferences. example, in eclipse, can enable feature from

  • window -> preferences -> java -> code style -> qualify generated field accesses 'this'

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -