#37 Static Variable in Java

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

Udemy Courses:

For More Queries WhatsApp or Call on : +919008963671

In this lecture we are discussing:
1)What is use static keyword in java?
2)At which place we can use static keyword. (discussing in upcoming next video)
3)when memory get allocated (extra information how static variable memory get allocated)
4)static variable vs instance method


#1
Use of static keyword
= when we are using static keyword with variable, block, method it become
independent of object.
e.g class{
static int a=5;
}
= a is not dependent on object .if we have two object obj1 and obj2 then both object able to access this
variable.
= when we are using static it become independent to object.

#2
Four place we can use static keyword
a) before variable declaration e.g class Calc{ static int s; }

c) during method creation class Calc{public static void add(int num1,int num2){};}

d) with concept of inner class (It is not discussed in this lecture)
class OuterClass {
int x = 10;

static class InnerClass {
int y = 5;
}
}

#3
When memory get allocated
= for that we need to know some term
a)stack area b)heap area c)class loader system
Step 1: when you compile the code you get .class file
Step 2: when you are executing (java MainClass) first class loading to class loader System.
Step 3: During class loading static variable initialize, static block get executed.
Step 4: Since, static variable got memory in heap before object creation. Now we can say that it is independent of object.

#4
Static variable vs instance variable

Class Calc{
static int stA=100; //independent of object // we can use this by class name as well as object
int inB=100; //dependent of object //we can only use this by object

public static void main(String []args){
Calc obj1=new Calc();
Calc obj1=new Calc();
//for static variable

//for instance variable

// = if we can change value static or instance what happen

//static
//it also show that static variable independent of objects

//instance
}
}


Remember one thing:
i)we can use static property, member inside non static block or method without object creation.
ii) But we cannot use non static property or method inside static block or method without object creation.

More Learning :

Donation:
PayPal Id : navinreddy20
Рекомендации по теме
Комментарии
Автор

Your explanation of static variable helps to understand static keywords better. Thanks.

MdRahaman-pjrc
Автор

Can I say a static variable is like a global variable? So local variables can only be shared/used among the instance/object. But a global variable can be used by the entire class and every instance/object belonging to that class?

sarthaknema
Автор

still can't believe you're providing this level of content free..

apoorvsingh
Автор

By the way price of an apple is lower than price of samsung... Must throw an error

nitinanand
Автор

Very helpful !
When creating the variable static name, you said that the name property of obj1 and obj2 will share the same static name, the static name is stored in an area in the heap memory, then the name attribute of obj1 and the name attribute of obj2 are in memory heap will contain the address of the static name in the heap memory right?

vanvietngo
Автор

thanks dear theacer your Student from Afghanistan

zobaidullahniazmand
Автор

But when we access static variable with object ref then we can change the value of static variable for all the objects then how its maintain rule of common for all (Belong to class ) or its just behaves like instance variable with hard core value ???? Is Memory saving the only difference ???

ashwintate
Автор

Sir I had an interview yesterday what is the significance of static variable .I don't know can you explain

swethasenthilkumar
Автор

Sir where the static variable is stored ?? Inside stack memory or Heap memory ?

preyas
Автор

Why are you not updating VS Code? Please update VS Code.

puruagni