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

Hi welcome to restaurant tutorials in this tutorial we are going to learn how static as well as non static method works why we actually make a variable static why we make a method static right and where do we actually use it so all of this will be discussed in this session okay so to understand this will create a.

New class I’ll show you an example and from that example you will get a good idea about where and why do we make a method or a variable static okay so I’m creating a new class here and let’s.
Say and name this class as planet okay.

Now in this planet class I’m creating two variables first one is let’s say it belongs to the string class and that shall give.

It name as name okay for the planet name and another variable uncrating shape okay these are my two variables now again I’ll create main method right here and in this main method I will basically initialize these variables or the string objects that I have created okay so suppose I told you that in order to use any property of a class we create its.

Object in the main so for the planet class again I’m creating its object so let’s say planet and I’m giving the object name as p1 initializing it okay so the object is ready now using this.

Object I can use this variable okay but right here let’s say I’ll create one more object okay p2 and I’ll initialize this also okay and let’s say create.

One more so a right here what I’m doing is within same class this planet class I created different objects for it p1 p2.

P3 okay now let’s just think about this as this okay suppose again this change the color suppose this is the object.

One let me get a better background here this is the p1 okay I have another object let’s say this is P 2 and this is the P 3 I’ll write down the names so we will get a better understanding P 2 and P 3 okay now each of these has its own name and shape variable for p2 also name and ship for p3 same name and shape this was a three.

Variables right for the class and for each of these objects there will be a different name and different shape variable so we start giving it name so I’ll say p1 dot name equals to earth right and that’s a P 2 dot name equals to Jupiter okay.

Same goes for P 3 not name equals to let’s say Pluto okay so what we did now we just gave this a name so P 1 name equals to earth okay this is Jupiter and this is Pluto okay this is.

What we did so far now same thing we’ll do for the shape variable or before going to.
The chappal I’ll tell you what some very.

Small concept if I print all of these to get it in the console to see it in the console what I’ll do is I’ll use a display statement same name of first planet is some name right so concatenated with the p1 dot name this is the name of us planet so this will print the name of first plane same thing I will do for the second planet object and same thing I’ll do for the third object just p2.

This is your second object this is the third one so now if I run this program I’m saying the name of first planet is Earth name.

Of second planet is Jupiter name of third planet is Pluto so all these are pending but now if I do a very small thing here and I say let’s say p1 is equal to p2.

Programming

Related Articles

0 Comment