Skip to content

Variables

Raycoms edited this page Jan 9, 2019 · 5 revisions

Naming

  • Named in lowerCamelCase convention.

Usage

  • Declare variables close to the location where they are used within Methods.
  • Fields are declared at the beginning of each class over the constructor.
  • Fields my references via this operator
/**/
public class MyClass
{
    private final int initial = 0;

    /**/

    public void someMethod( )
    {
        final int initial = this.initial;
        // Other method content
    }

    /**/
}
/**/

Receiving variables

  • If you get some getXY() method to store a reference to an Object, centralize the variable
/**/
public void someMethod( )
{
    final Object object = this.other.getObject();

    if ( checkCondition( ) )
    {
        object.someMethod( );
    }
    else {
        object.otherMethod( );
    }
}
/**/

Flags

  • Declare once instanciated variables as final.
/**/
public void someMethod( )
{
    final String neverChanged = "foobar";
    // Other method content
}
/**/