Types of Java Methods

Their Definitions and Purpose

0 Comments
Join the Conversation
Java Programming - Svilen Mushkatov
Java Programming - Svilen Mushkatov
Java methods are an integral part of the Java program, falling into a few categories like instance methods, class methods, helper methods, and constructors.

Many technical conversations about a Java program concern methods in some way. This focus is not overstated: all Java programs contain at least one method, the main method. All methods contain a heading and a block of code, and many receive and use parameters of some type.

Yet the methods available to the Java programmer falls into several descriptive categories: instance methods, class methods, helper methods, and constructors. All methods can also be organized according to whether or not they return a result from their operations.

Instance Method

When an object is instantiated, it is created based on its class definition. This object typically has instance fields, its own copied set of the standard fields of its class definition. Likewise, an instance method is associated with an object of a class rather than a class itself. For example:

System.out.println(“This is text.”);

Println is here an instance method associated with the out object that has been instantiated. The out object belongs to the System class; note that the more names tacked on to the right of the class name (System.out.println versus System.out) the more specific the reference becomes.

Class Methods

A method that belongs to its class rather than to an object of that class, as described above, is a class method. While an instance method has access to both instance fields and class fields, a class method has access only to the class fields. A class method is declared with a modifier, like so:

public static void methodName(){}

The static keyword sticks the method to the class rather than to objects of that class. When the class method is called, its name is tacked next to the class name rather than to an object of that name:

class.classMethod();

Class methods are useful for creating constants and class fields that need to be used throughout a program.

Helper Methods

Helper methods are exclusive to a class; only other methods in the same class can call this type of method. They are made exclusive with another use of declaring modifiers. Rather than the typical method heading of:

public void methodName(){}

Helper methods use the private keyword:

private void methodName(){}

Furthermore, a call to a helper method does not involve sticking the method name to a class name or object name as with instance methods and class methods. Rather than System.methodName();, a call to a helper method would simply be methodName();

Since the private method is in the same class and can only be used by methods in that class, there's no point in using the hierarchical referencing when calling these methods. Helper methods are typically used in complex classes that need careful organization to make sense to the programmer, or to perform tasks that are needed only in that class.

Constructors

Constructor methods are unique among methods, sharing little similarities and many differences with typical methods. First of all, constructor methods exist only to instantiate a class, not to execute code. Second, constructor methods have no return type at all, not even void, and have only one modifier in their heading:

public class constructorMethod(){}

Constructor methods must also be named exactly like their class, no variation at all. Variables must also be declared in a special way to avoid confusion with the class's instance variables, constructor methods have to be called in a special way, and so on.

While constructor methods are not required, a blank one is automatically loaded if none is present and multiple constructor methods with the same name can be written in a process known as overloading.

Summary

Methods, like many parts of Java, are a complex topic requiring a bit of time and study to understand completely. This article is intended to give the gist behind the concept of methods, to explain the different types of methods and provide a simple introduction to what they might look like in Java code.

Ana Mills, Ana Mills

Ana Mills - Ana Mills is an IT professional and freelance writer, with a Bachelor's of Business Administration in Computer Information Systems and ...

rss
Advertisement
Leave a comment

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
Submit
What is 2+3?
Advertisement
Advertisement