Statements


[Page 816 (continued)]

Declarations

There are two kinds of declaration statements: field declarations, which include a class's instance variables, and local variable declarations.

  • Put one statement per line, possibly followed by an end-of-line comment if the declaration needs explanation.

  • Initialize local variables when they are declared. Instance variables are given default initializations by Java.


  • [Page 817]
  • Place variable declarations at the beginning of the code blocks in which they are used rather than interspersing them throughout the code block.

The following class definition illustrates these points:

public class Example {     private int size = 0;     // Window length and width     private int area = 0;     // Window's current area     public void myMethod() {        int mouseX = 0;        // Beginning of method block        if (condition) {            int mouseY = 0;    // Beginning of if block        ...        } // if     } // myMethod() } // Example class 





Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net