Difference between static and non-static – Variables




Image Credit: Pixabay

Hi, welcome to the “non-static” tutorial 🙂  This lesson gives you answers for some things that I have skipped in the Beginner’s course.

Can you remember we have used the word “static” when starting a method or a member variable?

Eg:

Now, what happens if I remove this static part?? Let’s see…This tutorial will extend to the next one as well.

In this tutorial, I’ll only introduce non-static variables. There are non-static methods as well. They will be discussed in the next tutorial…

OK, here’s the example. We’ll make a new Class called Student.

In there, you can see, the variable passMark is have the word “static” and the other variables, name, registrationNo, age and examMarks don’t have that word.

Hence, all the other variables except passMark are called non-static variables. The difference in here is, static variables always belong to the Class where as non-static variables always belong to the Object.

This might confuse you 🙁 Don’t worry. It will be clear soon.

Now, how do we create a new Student object?

Let’s do that in the main() method of the Student Class.

Now, see, student1 is a variable of type Student!

The statement:

will create a new Student Object.

Congratulations! You have created your first (probably, if you are a beginner) custom type in java.

Creating a new Object is called “instantiation”. The student1 Object is called an “instance” of the Class Student. It’s just a word in programming. FYI 🙂

Just like the String type, you can say student1 is a variable of type Student.

To create a new Object of your own custom type, you have to always use this

  • new keyword

with the

  • name of the Class

and

  • the brackets.

We’ll further discuss about the constructing of an object later in Java Constructors lesson…

Now, it’s time to give our student1 a name and other details. This can be done as follows;

We can create another student variable student2. We can customize this variable by giving all details, same as the previous one.

 

Now, have you noticed that, we filled the details of the objects student1 and student2 to the variables which are non-static. See, name, age, examMark are all non-static. Also, notice that, these variables have different values in student1 and student2. They should be. Aren’t they? Two students should not have at least the same registration number in real world!

That means, the non-static variables belong to the object. They are defined in the Class Student. But see, they have different values assigned in the two objects.

You can easily print the values in the student objects like this…

You should now be able to see the difference between static and non-static. See in the declaration of variables, there’s a static variable passMark.

The passMark is the mark that should be fulfilled by any student to pass the exam.

So, passMark doesn’t change from one object to another. So, rather than make it non-static, we should make it static. When it is static, it belongs to the Class Student. Not to the objects Student1 or Student2. It’s common to both!

So, the value of passMark can be obtained by referring to the Class.

We can print this easily with the print statement.

POINT!

But what about this;

STILL CORRECT!

As I told you, passMark is common to all objects. So, student1 has a copy of that static value. So you can also call student1.passMark to get the passMark. But you should understand the meaning of this static. It is like a “public property” common for all objects 🙂

Now, here’s the complete code for Student Class we have studied so far…

Next, we’ll see how to write non-static methods…using the same Student class example.