#39 Static Block in java

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

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

For More Queries WhatsApp or Call on : +919008963671

In this lecture we are discussing:
1)What is use static keyword in java? (discussed in static varaible lecture)
2)At which place we can use static keyword.
3)when memory get allocated (discussed in static variable lecture)
4)static variable vs instance variable(discussed in static variable lecture)
5)static block vs non static method (Now discussed in this lecture)
6)static method vs non static method( will be discussed in static method lecture)

#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
}
}

#5
static block vs non static block
= remember static block executed before the execution of static method
= non static block executed when you try to create the object and executed before constructor called.

class Calc{
static{
}

{
//non static block
}

public static void main(String []args){
Calc c = new Calc(); //non static block executed when we create object
// Help h ; -- this will not execute static block of Help class
// Help h = new Help(); this will execute static block of Help class

}
}
class Help{
static {
}
static void wish(){
}
}

/*
PS D:\telusko\january\java-course-telusko\code java Calc
Static Block
Executed before main
main method
Non static block executed before the execution of constructor
Static block of Help class
Hello
*/

#6
Static method vs non static method

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.

Рекомендации по теме
Комментарии
Автор

mixing up many topics together make it look complicated, please make sure create a new class for every new topic you explain in separate eclipse in future for better understanding.
thank you so much.

ahmadfarhadzammani
Автор

02:55 .. I like the confidence, he knows the answer but plays around viewer thought as when he explain immideatly after that, you can see he knows very well.

fascinatingtop
Автор

Why does interest in Java keep increasing after watching this legend? 😂

MicronOfMotion
Автор

Thank you..this video makes sense. A static block is used to initialize static variables.

ajayghode
Автор

You always explain what happend and how happens you never explain the real purpose why there was need of that where we are using in the real case. Code will not be remember by anyone.

jatinsharma
Автор

you makes everything so much easier to understand and the syntax makes sense

jellyjollyjelly
Автор

wow, new courses!! very excited, thank you !!! im a amateur springboot dev, but i still scan through your videoes in this course and watch the ones which i feel i dont have enough knowledge on, and really get to know something new, like the contents of this video, the jvm and classloader explanation. thanks!! youre one of the best educators. PS: the order of static method and static block video is inverted

chinmayrath
Автор

Hi Naveen, really your knowledge on java is superb. How did you gain this knowledge? How long did it take to gain it?

padmini
Автор

when we are using Class.forName("className") to load the class,
we need to provide the fully qualified name of the class, including the package name. Then only it will load.
Also thanks for the amazing explanations. I check out your videos to quickly revise java concepts for interviews.

surabhigupta
Автор

Instead of creating a static block, why dont we directly initiate the static variables.
String name = "Phone";

sakthipriya
Автор

Every time you create object there are two steps 1) class loads 2) objs are instantiated.

ajayghode
Автор

Thank you Navin, Videos are helping me a lot to revise java concepts :)

sarikasona
Автор

I don't get it even if i don't call constructor the static should work since static is a class and it does not depend on the object right🤔 Please let me know if I'm wrong in any way.

subhparekh
Автор

This should be video #39. Learning about static method before static block is less confusing.

hameedferoz
Автор

How do I see the built in classes like you by just resting the pointer?

md.ikramulislam
Автор

@teusko it did call static block without creation of obejct

sriramkannan
Автор

Does constructor and static does not need data type ?

abheist_
Автор

Thank you but you maintion on last video you will show us static method but starting of this video you said Now we know static variable and static method. i did not see a video for static methods

ZeePatel-fj
Автор

Understood everything up until now but i don't know why this is giving me headache I am not getting it.
It makes code much more organized and easy to edit as all static methods and variables can be in one place and that executes first.

SanataniRishu
Автор

hi even though if i didnot create any object still static block is getting created can you explain this.

kesagaurav