
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…
1 2 3 4 5 6 7 8 9 10 11 |
public void printStudentDetails(){ System.out.println("Student Name : "+this.name); System.out.println("Registration No. : "+this.registrationNo); System.out.println("Age: "+this.age); System.out.println("Marks scored in the exam: "+this.examMarks); } |
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? 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class Student { public String name; public int registrationNo; public int age; public int examMarks; public static int passMark = 45; // NEW non-static method added public void printStudentDetails(){ System.out.println("Student Name : "+this.name); System.out.println("Registration No. : "+this.registrationNo); System.out.println("Age: "+this.age); System.out.println("Marks scored in the exam: "+this.examMarks); } } |
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…)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public static void main(String[] args){ Student student1 = new Student(); student1.name = "Raj"; student1.registrationNo = 10125; student1.age = 14; student1.examMarks = 76; //non-static method is called below: student1.printStudentDetails(); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public static void main(String[] args){ Student student1 = new Student(); student1.name = "Raj"; student1.registrationNo = 10125; student1.age = 14; student1.examMarks = 76; //non-static method is called below: student1.printStudentDetails(); //Creating a new Student object called student2 Student student2 = new Student(); student2.name = "Roy"; student2.registrationNo = 10157; student2.age = 15; student2.examMarks = 34; // printing details of student2 //non-static method is called below: student2.printStudentDetails(); } |
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…
1 2 3 4 5 6 7 8 9 10 11 |
public void printStudentDetails(){ System.out.println("Student Name : "+this.name); System.out.println("Registration No. : "+this.registrationNo); System.out.println("Age: "+this.age); System.out.println("Marks scored in the exam: "+this.examMarks); } |
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
1 |
System.out.println("Student Name : "+student1.name); |
Isn’t it?
But what happen if the method is called through the variable student2??
Then it should be
1 |
System.out.println("Student Name : "+ student2.name); |
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…
1 2 3 4 5 6 7 8 9 10 11 |
public static void isPassed(Student student){ if (student.examMarks>passMark) System.out.println(student.name+" passed the exam"); else System.out.println(student.name+" failed the exam"); } |
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;
1 |
Student.isPassed(student1); |
OK, see the difference between a static and non-static method. A non-static method is always called from an object.
1 |
student1.printStudentDetails(); |
But static method is not called by an object. It’s in the Class. So it’s called from the Student Class.
1 |
Student.isPassed(student1); |
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
1 |
System.out.println(Math.sqrt(10)); |
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;
1 2 3 |
String myStr = "Hello guys!"; System.out.println(myStr.toUpperCase()); |
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! 🙂