Difference between static and non-static – Methods




Static vs Non-static methods in java

Welcome back! Now in this tutorial, we are going to talk about the non-static methods used in java. In previous tutorial, since we have covered the non-static variables, you can easily understand what we are going to do. If you didn’t watch the previous one, I would recommend you to go and have a look coz we are going to use the same example used in the previous one…

Now, I’m going to add a non-static method for the Student Class…

So, the Class Student looks like this. I have removed the main() method here for you to understand the code easily. Without the main method, the Class Student will not run anything. You know that, right? 🙂

There’s a new word “this” is available in the println statement of the method. The meaning of “this” is explained below.

Let’s study this code carefully. First you can see that the static part of the declaration sentence of the method printStudentDetails(), is gone. It’s not available in here. But in the methods like main(), remember the declaration statement of main method? public static void main(String[] args) has the “static” part. So we say that, the printStudentDetails() is a non-static method whereas the main() method is a static method.

The method printStudentDetails() is intended to print the details of the Student objects. Now, we can call it in the main() method as follows;

(I’ll remove some of the code in main() method of the previous tutorial coz they are not required now…)

We have created a Student object called student1 and assigned all the details of that object. Now, the last statement, is a call for the method printStudentDetails();

OUTPUT:

Student Name : Raj

Registration No. : 10125

Age: 14

Marks scored in the exam: 76

 

Now, if you create another object student2 and call the student2.printStudentDetails(), it will print different information.

Let’s try it. I’ll create another Student object called student2.

OUTPUT:

Student Name : Raj

Registration No. : 10125

Age: 14

Marks scored in the exam: 76

Student Name : Roy

Registration No. : 10157

Age: 15

Marks scored in the exam: 34

Using the Dot

Have you noticed that we have called the method by putting .dot to a Student object?

student1.<See the dot>, it’s like this : student1.printStudentDetails().

So, the method should belong to the object rather than to the Class Student. That means, the method is called through the object. Not from the Class.

Now, it’s good to explain you the meaning of “this” statement. Let’s see our non-static method once again…

There, we can see, this.name, this.registrationNo, this.age and this.examMarks. What is this “this”?? 🙂

this” is a special keyword used to symbolize the object name. If we need to call this method, as I told you, we have to call it through a Student object, say, student1.

But can you remember, all the non-static variables name, registrationNo, age and examMarks are embedded inside the variable student1.

So, the print statement should actually be

Isn’t it?

But what happen if the method is called through the variable student2??

Then it should be

In fact, when we write this method, we actually don’t know for which variables will this method be called….

Therefore, we use the keyword “this”, in order to symbolize the variable name. When we use “this”, Java automatically replaces the word “this” with the actual variable name, when we call it. So this makes our programming life easy 🙂

Now I’m going to use a static method in the Class.

You may see the difference between the static and non-static methods very well if we also have a static method in the Class. So, here’s the code example for a static method…

This method requires a Student object as the parameter.

When you insert such object and call the method, it will check whether the student has got enough marks for the exam to pass.

This is done by comparing the examMark (non-static variable of the student object) with the static variable passMark.

Remember, the passMark is the minimum required mark to pass the exam.

Finally, it prints a message informing whether the student has passed or not.

This method isPassed() is a static one. See the keyword “static” on the declaration statement of the method.

Now, how can we call this?

In the main() method, we can do this;

OK, see the difference between a static and non-static method. A non-static method is always called from an object.

But static method is not called by an object. It’s in the Class. So it’s called from the Student Class.

Note that the student1 in the method isPassed() is just a parameter! It doesn’t involve in the static or non-static case. Just a parameter! (But you should, of course, define what is student1, just like any other parameter, before calling isPassed() method. You know that. Don’t you? 🙂

Need another example?

Can you remember the we used Math.sqrt() method to get the square root of a number?

Eg:

In the main() method, we can write

OUTPUT:

3.1622776601683795

 

Here, Math is just a Class. There’s a static method called sqrt(),requires an integer parameter and it calculates the square root of that parameter. Here, the parameter is 10. Then it returns the value. Here, it’s 3.1622776601683795.

See, this is called from the Math Class.

Now, let’s take a String example.

Eg:

Again, in the main() method;

See, toUpperCase() is not a static method. It’s non-static. It is called from the String variable myStr. NOT from the Class String like String.toUpperCase().

This is the nature of non-static world… Always go with the object!

In Object Oriented Programming, (in short, OOP) we are going to use these things almost everywhere in future codes. If you feel this part is too hard, don’t worry! You will catch this with the practice in no time! 🙂