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.

  • 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.
Link to original

Accessor methods

Accessor methods provide information about an object.

  • Has a return type that is not void.
  • Method will contain a return statement to return the value.
Link to original

Mutator methods

Mutator methods alter the state of an object.

  • Used to mutate / change an object’s state.
  • Often seen as “set” methods, which come in a simple form:
    • void return type
    • method name related to field name
    • single formal parameter with the same type as field
    • single assignment statement
Link to original

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.

Local variable

Methods can define their own local variables:

  • 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.
Link to original

Link to original

Conditional statement (Programming)

A conditional statement takes one of two possible actions based on the result of a test.

Link to original

0 items under this folder.