The Classes And Objects tutorial starts off gently enough but then gets increasingly more mind-bending as it dives into Inner Classes, Local Classes, Anonymous Classes, Lambda Expressions, Method References and introductions to Generics. It’s pretty long too so I’m splitting it out, starting with notes on the basics.


  • Class Declaration
class MyClass extends MySuperClass 
  implements YourInterface,AndYourInterface {
    // field, constructor, and method declarations
  • Variables
    • Member Variables : Fields
    • Method or Block Variables : Local Variables
    • Method Declaration Variables : Parameters
  • Field Declaration: modifiers type name;
  • Final Variables: can’t change value ( or object reference ) after it’s initialized ( but you can change the object itself ).
  • Constructors
    • no return type, e.g. public MyClass()
    • can be overloaded like methods
    • Implicit subclass of Object so has constructor by default.
  • Class Variables/Methods: static
  • Constants: static final type THIS_IS_A_CONSTANT
  • Constant Variable  : a variable of primitive type or String that is initialized with a compile-time constant expression
    static final int THIS_IS_A_CONSTANT = 1;
    • Compile-Time Constant: If the value is known at compile time, the compiler will replace all refs to primitive/string constants with the value meaning a change will require a recompile.


  • Initialize Class Variables
    • Static Initialization Blocks: Initialise class variables. Called in the order in the source code. Can be anywhere in the class body.
      static { // code for initialization goes here }
    • Private Static Method: Initialise class variables AND have has the advantage that can be reused.
      private static void initializeClassVariable() {}
    • Initializer Blocks
      • Alternative to constructor for initializing instance variables
      • Copied into every constructor so can be used to share code
        { // code for initialization goes here }
  • Final Method Instance Variable Initializer
    • Alternative to constructor for initializing instance variables
    • Subclass can reuse but not override
      protected final varType initializeInstanceVariable() {
              // code for initialization goes here
  • Access Modifiers
    • Classes can only be public or package-private
    • Members:
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier* Y Y N N
private Y N N N

* package-private

Use private unless you have a good reason not to. Avoid public fields except for constants.


  • Method Declaration
modifiers returnType methodName ( paramType paramName,...) exceptions {};
    • The returnType can be an Interface
    • The method can return a SubClass of the returnType ( this technique is called covariant return type)
    • The method can be overridden with a returnType that is a SubClass of the original
  • Method Signature
methodName (parameterType)
methodName (parameterType, parameterType ) <= overloaded method
  • Arbitrary Number of Arguments : varargs construct
methodName ( Type... arrayName) <= the ellipsis means n args will be
passes in an array, e.g. methodName ( Object1, Object2, Object...)
  • Method Parameter can be the same name as a Class field
class MyClass {
  private int x;
  public void aMethod ( int x ) {
    this.x = x; // In the method x, refers to the parameter x
  • Primitive types are Passed By Value
  • Reference types, i.e. Objects, are Passed By Value too, i.e. the reference


MyObject myObject = new MyObject(anArg); // new instantiates a class by 
           allocating memory for a new object and returning a reference
           to that memory
  • this : within an instance method or a constructor, this is a reference to the current object
  • explicit constructor invocation : use this to call another constructor in the same class
public Constructor() {
  this(arg); // The invocation of another constructor
                 must be the first line

public Constructor2(int arg) { ...


The exercise notes for this tutorial are here


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s