OCA Java SE 8 Mock Exam Review - Java Method Review

Methods with arguments and return values:

A method can accept method arguments.

A method may return a value.

A method returns a value by using the keyword return followed by the name of a variable, whose value is passed back to the calling method.

The return type of a method states the type that a method will return.

A method can have multiple parameters.

The method parameter can be a primitive type or objects of a class or interface.

The method parameters are separated by commas.

Each method parameter is preceded by the name of its type.

You can define only one variable argument in a parameter list with ... and it should be the final variable in the parameter list.

For a method whose return type is void, the return statement must not be followed by a return value.

If there is code that can be executed only after a return statement, the class will fail to compile.

If the return value from a method is assigned to a variable, the variable type should be compatible with the type of the return value.

Statements placed after the return statement aren't accessible and fail to compile.

Overloaded method

Overloaded methods accept different lists of arguments.

The argument lists can differ by

  • number of parameters that are accepted
  • types of parameters that are accepted
  • positions of parameters that are accepted

Methods can't be defined as overloaded methods if they differ only in their return types or access modifiers.


Constructors create and return an object of the class in which they're defined.

Constructors have the same name as the class.

Constructors don't specify a return type-not even void.

Default constructors are defined by Java, if the developer doesn't define any constructor in a class.

You can define a constructor using the four access modifiers:

  • public,
  • protected,
  • not specifying any modifiers, and
  • private.

If you define a return type for a constructor, it'll no longer a constructor. It'll be a regular method, even though it shares the same name as its class.

An initializer block is defined within a class, not as a part of a method. It executes for every object that's created for a class.

If you define both an initializer and a constructor for a class, both of these will execute.

The initializer block will execute prior to the constructor.

An initializer block can't accept method parameters.

An initializer block can create local variables.

An initializer can access and assign values to instance and static variables.

An initializer can call methods and define loops, conditional statements, and try-catch-finally blocks.

Overloaded constructors

A class can define overloaded constructors using different argument lists.

Overloaded constructors may be defined using different access modifiers.

A constructor can call another overloaded constructor by using the keyword this.

A constructor can't invoke a constructor by using its class's name.

A call to another constructor should be the first statement in a constructor.

Object fields/instance variable

An object field can be read by either directly accessing the variable or by using a method that returns its value.

An object field can be written by directly accessing the variable or by using constructors and methods that accept a value and assign it to the instance variable.

You can call methods defined in a class using an object reference variable.

Passing objects and primitives to methods

When passing a primitive variable to a method, its value remains the same after the execution of the method.

When passing an object to a method, the method can modify the object's state by executing its methods.

Comparing objects for equality

The method equals is defined in the class java.lang.Object.

All the Java classes directly or indirectly inherit java.lang.Object.

The default implementation of the equals method only compares whether two object variables refer to the same object.