Java Instance Methods
As you know, a class is one of the most fundamental concept in object-oriented programming languages, such as Java. You develop the logic of a program by creating fields and methods inside classes. Fields describe an object's properties and methods describe what an object does. In this topic, we will go on to discuss the nature of methods in Java. All methods can be divided into two groups: instance and static. We were mostly focused on the static ones previously, so now it's time to learn more about instance methods to better understand the differences between the two.
What's the difference?
Let's look at the code below. Here we have a class named Human
with two fields and two methods.
class Human {
String name;
int age;
public static void printStatic() {
System.out.println("It's a static method");
}
public void printInstance() {
System.out.println("It's an instance method");
}
}
The modifier public
isn't important for us now. It just means that other classes can also get access to our methods.
As you see, the methods printStatic
and printInstance
have differences in declaration. When you see a method, you can easily understand that if there is the static
keyword, then the method is static; if there is no static
keyword, then the method is an instance method.
Now let's see what this really means!
Understanding: static and instance
To invoke a static method we don't need to create an object. We just call the method with the class name.
public static void main(String[] args) {
Human.printStatic(); // will print "It's a static method"
}
In other words, you can say that a static method belongs to a class (because we don't need an object).
An instance method requires a different invocation. As you already guessed, to invoke an instance method we have to create an object first. Otherwise, there is no way to use an instance method.
It's called an instance method because an instance is a concrete representation of an object.
Here we call the method printInstance
for two different objects:
public static void main(String[] args) {
Human peter = new Human();
peter.printInstance(); // will print "It's an instance method"
Human alice = new Human();
alice.printInstance(); // will print "It's an instance method"
}
So, we can say that an instance method is a method that belongs to each object that we created using the particular class.
Instance methods: features
Instance methods have a great advantage: they can access fields of the particular object of the class.
To illustrate the feature, let's modify our Human
class. We have one static method averageWorking
and two instance methods: work
and workTogetherWith
.
class Human {
String name;
int age;
public static void averageWorking() {
System.out.println("An average human works 40 hours per week.");
}
public void work() {
System.out.println(this.name + " loves working!");
}
public void workTogetherWith(Human other) {
System.out.println(this.name + " loves working with " + other.name + '!');
}
}
The keyword this
represents a particular instance of the class.
It's easier to understand by an example:
public static void main(String[] args) {
Human.averageWorking(); // "An average human works 40 hours per week."
Human peter = new Human();
peter.name = "Peter";
peter.work(); // "Peter loves working!"
Human alice = new Human();
alice.name = "Alice";
alice.work(); // "Alice loves working!"
peter.workTogetherWith(alice); // "Peter loves working with Alice!"
}
Look, now we have different outputs for the work
method because two different objects have different values for name
. First, we created peter
and gave the object a name. Then by invoking peter.work()
we actually saw the assigned name in the output. We did the same with alice
. Invoking the alice.work()
method gave an output which also contained the name assigned to alice
.
In the workTogetherWith
method, this
keyword allows us to access a field of the particular object and distinguish it from the same field of another object.
In this case, this
keyword is optional, so the code will work the same without it. But it's a good practice to add it to avoid confusion.
Of course, instance methods can take arguments and return values as well. Return values can be of any type including the same type as the defined class.
Summary
In Java, every method should be declared within a class. The key difference between instance and the static methods lies in whether they interact with an object or not. Let's recap:
- static method is associated with the class as a whole;
- an instance method can only be invoked through an instance of a class, so that you have to create an object first;
- instance methods can access the fields of the class using
this
keyword.
Instance methods allow programmers to manipulate particular objects of a class. And because of it, they give us more functionality and are used more often than static methods!