#37 Static Variable in Java

  Рет қаралды 123,919

Telusko

Telusko

Күн бұрын

Check out our courses:
Spring and Microservices Weekend Live Batch : bit.ly/spring-live-weekend
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : bit.ly/java-spring-cloud
Udemy Courses:
Java:- bit.ly/JavaUdemyTelusko
Spring:- bit.ly/SpringUdemyTelusko
Java For Programmers:- bit.ly/javaProgrammers
For More Queries WhatsApp or Call on : +919008963671
website : courses.telusko.com/
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; }
b) before a block class Calc{ static {System.out.println("this is static block);}}
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
System.out.println(Calc.stA); //use with class name
System.out.println(obj1.stA); //use by object name
//for instance variable
//System.out.println(Calc.inB); //got an error --Cannot make a static reference to the non-static
System.out.println(obj1.inB); //used by object name reference
// = if we can change value static or instance what happen
obj1.inB=1000;
obj1.stA=2000;
//static
System.out.println(obj1.stA);//output: 2000
System.out.println(obj2.stA);//output: 2000 value changed for both obj1 and obj2
//it also show that static variable independent of objects
//instance
System.out.println(obj1.inB); // output: 1000
System.out.println(obj2.inB); // output: 100 no change in obj2
}
}
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.
Github repo : github.com/navinreddy20/Javac...
Java:- bit.ly/JavaUdemyTelusko
Spring:- bit.ly/SpringUdemyTelusko
More Learning :
Java :- bit.ly/3x6rr0N
Python :- bit.ly/3GRc7JX
Django :- bit.ly/3MmoJK6
JavaScript :- bit.ly/3tiAlHo
Node JS :- bit.ly/3GT4liq
Rest Api :-bit.ly/3MjhZwt
Servlet :- bit.ly/3Q7eA7k
Spring Framework :- bit.ly/3xi7buh
Design Patterns in Java :- bit.ly/3MocXiq
Docker :- bit.ly/3xjWzLA
Blockchain Tutorial :- bit.ly/3NSbOkc
Corda Tutorial:- bit.ly/3thbUKa
Hyperledger Fabric :- bit.ly/38RZCRB
NoSQL Tutorial :- bit.ly/3aJpRuc
Mysql Tutorial :- bit.ly/3thpr4L
Data Structures using Java :- bit.ly/3MuJa7S
Git Tutorial :- bit.ly/3NXyCPu
Donation:
PayPal Id : navinreddy20
www.telusko.com

Пікірлер: 20
@MdRahaman-pj9rc
@MdRahaman-pj9rc Жыл бұрын
Your explanation of static variable helps to understand static keywords better. Thanks.
@sarthaknema1130
@sarthaknema1130 6 ай бұрын
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?
@ahmadwaheed8003
@ahmadwaheed8003 Жыл бұрын
Great explanation
@zobaidullahniazmand4375
@zobaidullahniazmand4375 Жыл бұрын
thanks dear theacer your Student from Afghanistan
@user-nd7pw9gc1w
@user-nd7pw9gc1w Жыл бұрын
nice explanation sir
@spiderman699
@spiderman699 5 ай бұрын
Well explained 🎉❤
@vanvietngo4217
@vanvietngo4217 Жыл бұрын
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?
@MoeMoe-qr7od
@MoeMoe-qr7od 5 ай бұрын
excellent explain static
@Divyasri1922
@Divyasri1922 6 ай бұрын
Well explanation
@nitinanand3408
@nitinanand3408 Жыл бұрын
By the way price of an apple is lower than price of samsung... Must throw an error
@MoeMoe-qr7od
@MoeMoe-qr7od 5 ай бұрын
wkwk
@ashwintate1009
@ashwintate1009 Жыл бұрын
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 ???
@ahmadwaheed8003
@ahmadwaheed8003 Жыл бұрын
Yes we can access with object reference but it will reflect in all the objects so it is called common area.
@ahmadwaheed8003
@ahmadwaheed8003 Жыл бұрын
Yes when we make a common variable for all the objects we don't need to initialize it with each object so memory saving is the advantage of using static.
@swethasenthilkumar6372
@swethasenthilkumar6372 7 ай бұрын
Sir I had an interview yesterday what is the significance of static variable .I don't know can you explain
@alinakhan1159
@alinakhan1159 4 ай бұрын
less memory is used for instance if you declare 3 non static integer variables then each object will occupy 12 bytes of memory ..if there are 3 objects total will be 36 but if you declare one static variable and if the other 2 are non static then static variable will take its 4 bytes which will be shared with all objects and each object will separately take its 8 bytes of storage..so total memory that is used for 3 objects will be 24 bytes and 24+4 is 28 bytes...
@preyas2198
@preyas2198 Жыл бұрын
Sir where the static variable is stored ?? Inside stack memory or Heap memory ?
@neerajgarg9096
@neerajgarg9096 9 ай бұрын
heap memory
@Nikita-qc7dp
@Nikita-qc7dp 5 ай бұрын
Inside static nd global memory of heap
@puruagni1927
@puruagni1927 2 ай бұрын
Why are you not updating VS Code? Please update VS Code.
#38 Static Method in Java
4:44
Telusko
Рет қаралды 129 М.
#26 Stack And Heap in Java
12:37
Telusko
Рет қаралды 207 М.
Nutella bro sis family Challenge 😋
00:31
Mr. Clabik
Рет қаралды 12 МЛН
LOVE LETTER - POPPY PLAYTIME CHAPTER 3 | GH'S ANIMATION
00:15
#46 Anonymous Object in java
4:48
Telusko
Рет қаралды 55 М.
#36 StringBuffer and StringBuilder in Java
5:41
Telusko
Рет қаралды 157 М.
#40 Encapsulation in Java
11:42
Telusko
Рет қаралды 146 М.
#39 Static Block in java
7:17
Telusko
Рет қаралды 95 М.
#35 Mutable vs Immutable String in Java
6:23
Telusko
Рет қаралды 104 М.
#43 Constructor in Java
7:05
Telusko
Рет қаралды 136 М.
#52 Method Overriding in Java
7:57
Telusko
Рет қаралды 117 М.
#48 What is Inheritance in Java
8:47
Telusko
Рет қаралды 108 М.
Todos os modelos de smartphone
0:20
Spider Slack
Рет қаралды 660 М.
Samsung Galaxy 🔥 #shorts  #trending #youtubeshorts  #shortvideo ujjawal4u
0:10
Ujjawal4u. 120k Views . 4 hours ago
Рет қаралды 3,9 МЛН
1$ vs 500$ ВИРТУАЛЬНАЯ РЕАЛЬНОСТЬ !
23:20
GoldenBurst
Рет қаралды 1,6 МЛН
Klavye İle Trafik Işığını Yönetmek #shorts
0:18
Osman Kabadayı
Рет қаралды 1,3 МЛН