Basics Of Java Programming|static Vs Non Static Variable-methods In Java

So what will happen now before you know running this program I’ll.

Show you the illustration again earlier it was like this p1 was pointing towards this memory location which has name stored as.

Earth and she appeared in sight yet here p2 is pointing towards nameko so Jupiter and PC name equals to Pluto ok but now when I am writing P 1 is equals to P 2 that means B even no longer is pointing towards this memory location ok now P 1 is start pointing towards the memory location where P 2 is pointing so now if I run this program.

Again ok and check it out these outputs again you will see a change in the name of object p1 ok so earlier it was like this but now name of us planet is also showing as Jupiter why because I said object P 1 is equals to.

Means P 1 is not – pointing towards this memory location which was pointing earlier but now it’s pointing towards her p2 is pointing okay okay now I’ll if.

I’ll talk about one thing more one variable more that we have declared which is shape if I set the shape again I will follow the same drill what I will do eros again I’ll say even dot shape equals two circle okay P 2 dot shape again this planet is also circular in shape so I’ll say circle and let’s say p3 is also equals to.

Circle right same thing I can print name of the first planet as this and let’s say again concatenate it and say shape is just again you have to add that variable size up even dot shape and this is the same thing I’ll do for the other.

Variables too okay so here I’ll set p2 p3.

Wondering it doesn’t make any difference if L still say p1 because all the shapes are similar so that’s the exact my point that exactly is my point if I.

Run this now you will see the shape is circle shape a circle shape a circle so that means all of the three three this object has one different property and one similar property like all of these three objects have different names that’s why we created this different object but this shape property is same among them among all these planet objects okay so what we are doing right here it’s not correct you’re just wasting the memory space again again you know allocating the same thing to the.

Same object so how we can make this.

Better from the illustration itself I will show you again so this is now it’s happening but this shape is same so why don’t we do something this is a circle this is also circle and this.

One is also circle so why don’t we you know create a memory space such a way here that is shared among all of these okay suppose this point towards this this point towards this.

And here I will store that value of that shape equals to circle so all have the access to it okay this is the right way of doing what we are doing right here it’s not correct because all this planet have same shape so it’s it’s.

Not a good practice to write the you know the same code again and again so I can do this by making this shape variable static just season when I will make it static okay I have I am no longer to give it you know p1 dot shape again and again I can simply say shape.

Equals to circle okay and I can directly use the shape when I’m printing the output you see I’m not getting any error.

But if I don’t make it static it this code won’t work we’ll get an error cannot make a static reference to a non-static field so whatever fields that we are created this.

Is the non static one when we created static it it no longer require you know dependent on the object of the class its independent now it’s now dependent on.

The class itself not on the object so we can directly use it so now I will run this we’ll see the same output okay but this.

Time we did it in the right ways circle was a common property among all these planet class objects okay so that’s why I made it static okay so you know this is why we create the static variable now we’ll see how and why we create the static method okay so we have just discussed how a static variable works or when.

We make a variable static from this example now we will discuss about static methods okay so with the same example if I create a function here I ordered already told you learn how we create a function so in.

This class I’m creating a function called let’s say public void print name so basically it’s going to print the.

Planet’s name according to its object right so I’ll say system dot out and I will print and print the name of the planet okay via this function now so here I will just simply say name and I will create another method here public void let’s say reward it’s another function of the planet class I’m naming it as it is revulsion and.

This again I’m print putting a display statement let’s say planet is revolving okay around Sun it’s a simple function now both of these methods are non-static right or just forget the name static these are the normal function or the method that we are creating earlier so.

In order to use this method what we have to do it is use the object planet object let’s say I’ll use p1 and call it function so the function first function is named print name right so.

I’ll just simply use this that function for p1 for p2 also I’ll use this function same goes for the p3 okay I’ll comment out other things so we’ll just see this in the output okay same I’ll do for.

The other function we have another function called a revolt so I call the revolt function with object p1 now with object b2 now with object p3 okay simple as that now if I run this class in the output I’ll see name of the planet like earth Jupiter Pluto and planet is revolving now the output of the second function that we have created revolve is similar for all.
The objects or for all the planet class object output is same so again you.

Know why we made the state variable static shape variable because it’s it was a common property now this is a common functionality we created a function that is used by each object of this planet class and it’s again a you know common functionality so why don’t I make this as static so now if I write static in front of it there is no need to call this function.

Three times right I can simply call the Raval without.

Any object I won’t see any error if I run this I will see this in the output I can call it multiple time as.

Programming

Related Articles

0 Comment