Basic class structure
Typical structure of a class.
public class ClassName {
// Fields
// Constructors
// Methods
}Reserved keyword
Reserved keywords are words with special meaning in the language that cannot be used as variable names and are usually always lowercase.
Link to original
Methods
Method
A method is a procedure on an object which can be invoked. A method may have parameters to pass additional information. All methods have return types / values, a method may return void if no information is being handed back to the caller.
A method implements the behaviour of objects, they have a consistent structure of a header and a body.
Link to original
- The header describes:
- The visibility to objects of other classes.
- Whether the method returns a result.
- The name of the method.
- Whether the method takes parameters.
- The body encloses the body’s statements.
Accessor methods
Accessor methods provide information about an object.
Link to original
- Has a return type that is not void.
- Method will contain a return statement to return the value.
Mutator methods
Mutator methods alter the state of an object.
Link to original
- Used to mutate / change an object’s state.
- Often seen as “set” methods, which come in a simple form:
voidreturn type- method name related to field name
- single formal parameter with the same type as field
- single assignment statement
Protective mutator
A protective mutator protects fields by being the only way to set a value to a field, it can check the parameter for validity and throw an error if it is incorrect.
Link to original
Variables
Variables
Each block defines a new scope, such as a class, method or statement. Scopes may be nested inside of one another, and they are static. Scope lifetime is dynamic during runtime.
Link to originalLocal variable
Methods can define their own local variables:
Link to original
- Short lived, like parameters.
- Method sets their values.
- Used for temporary calculation and storage.
- Exist only as long as the method is being executed.
- Only accessible from within the method.
- Only defined within a particular scope.
Conditional statement (Programming)
A conditional statement takes one of two possible actions based on the result of a test.
Link to original