Skip to main content
OCA Java Programmer 1Z0-803

JAVA Call methods on objects

By November 10, 2013September 12th, 2022No Comments

We have already been able to access methods on objects that we have created and seen how we run or call those methods. In this tutorial we want to make sure that you are getting yourself fully prepared for the OCA exam 1Z0-803. The JAVA 7 SE Programmer 1 certification. To that end we will look a little more at anonymous instantiation of our objects and delve into some more detail of the method signature.

Anonymous Instantiation

If we need to access an object to quickly read a field or call a method and we only need it for that one task then there is no requirement to create an object reference; we can access the object anonymously or without a variable identifier.

Instantiation using an Identifier and method calling:

ObjectLife of = new ObjectLife();
 of.sayMessage();

Anonymous instantiation and method calling:

new ObjectLife().sayMessage();

In addition, as we do not have a variable to reference the object ; it is immediately de-referenced and the memory used available to the garbage collector.

Calling Methods

When calling a method we would need to know the method signature in order to know what arguments need to passed through to method. Also we would need to know if the method was able to return values and the data type returned. The great feature of an IDE such as Eclipse or NetBeans is that it will display this information to you in the Intellisense console. Methods signature with a void modifier do not return data, anythinh other than void then the method will have a return statement that will rerun data of the type indicated. In the example we look at two methods. One that returns an integer and requires two arguments, both integers; the second takes no args and does not retrun data.

Calling a method with arguments that returns data

int area = of.area(3, 5);

Calling a method without arguments that returns no data

of.sayMessage();