7.16 How to use Static Block in Java Tutorial

  Рет қаралды 136,106

Telusko

Telusko

Күн бұрын

Printing Something without any statement in Main Method.
Static Blocks are called before main Method.
Static Blocks are called as soon as your class is loaded in JVM.
Static block is a block of code which is preceded by the static keyword. Static block are also called
initialization block .It can be used to print something without any statement in Main Method.
Static Blocks are called before main Method.
Static Blocks are called as soon as your class is loaded in JVM.
Check out our website: www.telusko.com
Follow Telusko on Twitter: / navinreddy20
Follow on Facebook:
Telusko : / teluskolearnings
Navin Reddy : / navintelusko
Follow Navin Reddy on Instagram: / navinreddy20
Subscribe to our other channel:
Navin Reddy : / @navinreddy
Telusko Hindi :
/ @teluskohindi
subscribe to the channel and learn Programming in easy way.
Java Tutorial for Beginners: goo.gl/p10QfB
C Tutorial Playlist : goo.gl/8v92pu
Android Tutorial for Beginners Playlist : goo.gl/MzlIUJ
XML Tutorial : goo.gl/Eo79do
Design Patterns in Java : goo.gl/Kd2MWE
Socket Programming in Java : goo.gl/jlMEbg
Spring MVC Tutorial : goo.gl/9ubbG2
OpenShift Tutorial for Beginners : goo.gl/s58BQH
Spring Framework with Maven : goo.gl/MaEluO
Sql Tutorial for Beginners : goo.gl/x3PrTg
String Handling in Java : goo.gl/zUdPwa
Array in Java : goo.gl/uXTaUy
Java Servlet : goo.gl/R5nHp8
Exception Handling in Java : goo.gl/N4NbAW

Пікірлер: 80
@javadoctor101
@javadoctor101 6 жыл бұрын
Lol..someone is doing pooja in the background..desi will relate..nice tuts btw..
@thestarinthesky_
@thestarinthesky_ 4 жыл бұрын
Thank you Telusko. You have done a great job!I learned a lot.
@karrinagasatyanarayanaredd1921
@karrinagasatyanarayanaredd1921 3 жыл бұрын
sachin tendulkar: made batting look so easy .. navin reddy: made java look so easy
@mayankmaheshwari6400
@mayankmaheshwari6400 6 жыл бұрын
Would be better if you can increase your pitch a bit. With my laptop volume full, still I am facing issues in hearing!
@Sunny_sharma007
@Sunny_sharma007 5 жыл бұрын
Navin sir you r awesome😃😃😃😀apun ko programming smj arahi ha ab 😄😄Thank you so much
@swaminathbera6407
@swaminathbera6407 2 жыл бұрын
Does this mean static block is always called first, even before main(). It was interesting to know :)
@prasadsardesai
@prasadsardesai 6 жыл бұрын
You made java very easy for me Thanks 🙏
@tissuegameshow
@tissuegameshow 7 жыл бұрын
Great Tutorial !!
@jww7663
@jww7663 5 жыл бұрын
thank you, it is very helpful
@sweatheart786
@sweatheart786 5 жыл бұрын
we can initialize static variable in main method too.
@user-cc4si1rp7w
@user-cc4si1rp7w Жыл бұрын
super helpful sir, thanks
@deepaligupta9031
@deepaligupta9031 3 жыл бұрын
But in the previous lecture 7.15 i.e. count number of object, constructor worked on static variable as it has increases the count of i. Please elaborate
@Tali1562
@Tali1562 9 жыл бұрын
Thanks!
@somsuvrochakraborty9853
@somsuvrochakraborty9853 8 жыл бұрын
Thanku..a lot
@hhhuhhh5692
@hhhuhhh5692 2 жыл бұрын
thanks a lot!
@anirudhbhardwaj6834
@anirudhbhardwaj6834 4 жыл бұрын
Thank you
@pulkitsharma5792
@pulkitsharma5792 3 жыл бұрын
But we can initialize the static variable at the time of declaration itlself? Why we need stattic block
@amitashah3711
@amitashah3711 6 жыл бұрын
Simple question .first without writing any static string both static block executed 1st & then main,& after using static string first main get executed then static block why??
@souvikpal6262
@souvikpal6262 7 жыл бұрын
@ 5:30 you said we cannot use constructor for manipulating static variables , but as constructor is non - static member method we can access a static variable within a non-static member function, isn't it ????????????????????????????
@furyperry4374
@furyperry4374 7 жыл бұрын
YES YOU CAN USE IT package Apple; public class banana { static String s= "hello world"; banana() { s="martha"; } public static void main(String[] args) { banana x=new banana(); System.out.println("the value of s is " +s); } } OUTPUT: the value of s is martha. although the creation of object was just to initialize the constructor , so that we could assign "martha" to the String s
@thestarinthesky_
@thestarinthesky_ 5 жыл бұрын
It means that in general for initializing static variables, you don't have to use constructor, which is generally used to initialize instance variables. In this example, we don't have to create an object and then call constructor to initialize the static variable. We can simply do that by static block.
@mohanishsaim6299
@mohanishsaim6299 4 жыл бұрын
@@thestarinthesky_ Thanks
@abdelde8553
@abdelde8553 4 жыл бұрын
Hello; I do really appreciate your tutorials but i have a short question In this video you said that constructors only works on instances variables but in the last video about counting number of objects we used a class variable to count number of objects ( static int i)
@villagecoder
@villagecoder 4 жыл бұрын
If I understand you, you mean there was a code in the constructor that did counting or arithmetic. You should understand that constructor is a method, a special one, so you can execute any block of code within. This block of code will be executed while an instance of the class is created. You should understand that constructors do not only initialize fields when creating instances but they can even call methods to perform some operation in the course of creating an object. In the case of static blocks, they are also called static initializers because they only intialize static members. Constructors initialize all fields. Look at this example: public class Test{ int a, b, c; //Constructor public Test(int a, int b){ this.a=a; setB(b); //setB method called in a constructor c+=4; //arithmetic in constructor } public void setB(int num){ b= Math.abs(num); }
@naoyoshino7014
@naoyoshino7014 3 жыл бұрын
Is it compulsory to write main function? Because static is already writing hello world
@TheSumit22
@TheSumit22 Жыл бұрын
so most of your examples can be executed in the static block itself, with a blank main method. what are the intended application of these static blocks?
@aarushigupta1106
@aarushigupta1106 9 жыл бұрын
thanks for this video but plzz tell can we not use static methods to give a value to static variables if yes whats need of static block
@coolrudra2529
@coolrudra2529 7 жыл бұрын
you can correlate it to "the purpose of default constructor", like we have constructor to initializing the instance variable , likewise static block is there for initializing static variable to any particular value
@Coalpot-Jollof
@Coalpot-Jollof 6 жыл бұрын
Imagine the static block as an initializer of static fields. It can be used for different stuff as the programmer wishes
@abhishek_sengupta
@abhishek_sengupta 4 жыл бұрын
Can't we directly initialize a static member without using static block?
@sohampal759
@sohampal759 4 жыл бұрын
public class StaticBlock { static String s = "Hello world"; static { // System.out.println("Hello world"); } public static void main(String args[]){ System.out.println("the value for s "+s); } } this runs...
@abhishekabhi732
@abhishekabhi732 4 жыл бұрын
Then we can write out code in static only no sir? Why we can write in public static void main?
@mohitmishra8100
@mohitmishra8100 5 жыл бұрын
can we do multiplication like 10*5 without using any arithmetic operator?
@rahulpradhan5585
@rahulpradhan5585 4 жыл бұрын
Yes With the help of recursion a=10; b=5; Int product=0; for(int i=1; i10+10+10+10+10=10*5 I hope u get the point
@avineshtripathi7458
@avineshtripathi7458 3 жыл бұрын
@@rahulpradhan5585 exactly u used to the arithmetic operator so try using increment operator by logic that a method favorable for ur situation
@unmeshchougule5666
@unmeshchougule5666 2 жыл бұрын
Your explanations are very good but we need more practical examples apart from demo and hello world 😃
@tanmaykumar6856
@tanmaykumar6856 5 жыл бұрын
sir the volume in the video is a bit on the lower side
@harisankarl6292
@harisankarl6292 8 жыл бұрын
nice
@krishnadaram1568
@krishnadaram1568 3 жыл бұрын
Could you explain non static block.. Sir!!
@krishnapritampaladhi7509
@krishnapritampaladhi7509 3 жыл бұрын
you made me a programmer
@i_karank
@i_karank 7 жыл бұрын
cant we initialize static variable s with "hello world" during its declaration?
@sayanmukherjee8563
@sayanmukherjee8563 7 жыл бұрын
Yes, we can.I have run the code initializing s while declaration.No error found, works fine.
@snehayannam6872
@snehayannam6872 9 жыл бұрын
sir, here 1st the static block will be exectued, then main block will be executed no sir so the output has to be hello world the value of s: hello world
@swarnaraha7166
@swarnaraha7166 8 жыл бұрын
+sneha yannam i also have the same confusion
@arveersingh9728
@arveersingh9728 8 жыл бұрын
its because we declared the value of s but we didn't print it out in the first place.
@gauravpande
@gauravpande 7 жыл бұрын
I like your confidence :)
@furyperry4374
@furyperry4374 7 жыл бұрын
This would have been the case if we would have Printed the value of the static variable s in the static block, but we are just assigning it a value . i.e firstly s is assigned the value : hello world and then when main method is executed , it prints outs the value
@daakshayaniallu2277
@daakshayaniallu2277 8 жыл бұрын
shouldnot we assign the value for s =helloworld without using staticblock
@krishaengineer9754
@krishaengineer9754 7 жыл бұрын
same question
@coolrudra2529
@coolrudra2529 7 жыл бұрын
you can correlate it to "the purpose of default constructor", like we have constructor to initializing the instance variable , likewise static block is there for initializing static variable to any particular value
@lh6845
@lh6845 3 жыл бұрын
What happens if a class extends a parent class? Does the parents class get loaded what if the parent extends another class? What if that class has static and init methods?? In what order will it execute?
@unmeshchougule5666
@unmeshchougule5666 2 жыл бұрын
Just try it yourself, its easy 😉
@mohamedthoufiqm8108
@mohamedthoufiqm8108 Жыл бұрын
❤️
@senurachamuth
@senurachamuth 4 жыл бұрын
public class A { static int x; A() { System.out.println("Constructor"); } static { x = 7; System.out.println("Static 1");} {x = 8; System.out.println("Static 2");} public static void main(String[] args) { System.out.println(x); A a = new A(); System.out.println(x); } } how this happnd
@JP-yf2kp
@JP-yf2kp 7 жыл бұрын
constructor only works on instance variable is wrong buddy. constructor also works on static variable.we can do it without static block using constructor.
@md.adittorehan6389
@md.adittorehan6389 3 жыл бұрын
Yes. But I think we have to make object of that class in another class to do that.
@maxkaraev7306
@maxkaraev7306 4 жыл бұрын
You need to keep your voice level constant! buddy. I tried to watch this but couldnt.
@geethamadhurivasa5779
@geethamadhurivasa5779 4 жыл бұрын
whats the output public class Main { static String s=""; static{ s="hello world"; } public static void main(String[] args) { System.out.println(s); } static{ s=""; } static{ s="abcd"; } static { s="efgh"; } } first guess and try it in compiler
@SunilKumar-cu8wm
@SunilKumar-cu8wm 9 жыл бұрын
I think there'll be no simpler way to understand use of static block...
@Coalpot-Jollof
@Coalpot-Jollof 6 жыл бұрын
There is a way to understand. DONT BE LAZY
@sapnavats9105
@sapnavats9105 6 жыл бұрын
We can also initialize a static variable without using static block in a normal fashion. So what is the requirement of static block?
@Coalpot-Jollof
@Coalpot-Jollof 6 жыл бұрын
If you need to execute some code before the creation of an object then use the static block
@ashishsaini2765
@ashishsaini2765 5 жыл бұрын
Static block is used to initialize static variable dynamically
@vipulshah7091
@vipulshah7091 3 жыл бұрын
Sir can you please explain that why do we use a static block?
@yashroushan5543
@yashroushan5543 Жыл бұрын
4:22
@indiagood3667
@indiagood3667 6 жыл бұрын
somebody is ringing bell.
@Chandrikareddyy
@Chandrikareddyy 9 жыл бұрын
what happens if i say my class as static? as we do in the abstraction of class ? can it turn all the methods of class into static?? A static{ m(){ print("say y or no ") } } i tried but showing a error saying you already declared static ???
@gauravpande
@gauravpande 7 жыл бұрын
I don't think you can make your class static, but can make an inner class static
@thilinsmsnoj
@thilinsmsnoj 4 жыл бұрын
why are u doing static in your class , what is the reason ?
@susobhandas6143
@susobhandas6143 4 жыл бұрын
why you make videos with bad sound
@susobhandas6143
@susobhandas6143 4 жыл бұрын
bad sound in this particular video
@ieftnrightasmr9662
@ieftnrightasmr9662 Жыл бұрын
lol someones doing puja
@pranabroy8218
@pranabroy8218 3 жыл бұрын
My whole programming life was a lie.
@Draven1334
@Draven1334 4 жыл бұрын
dislike for the annoying sound in the background
@sasmalpayal
@sasmalpayal 7 жыл бұрын
Can we create a method within a static block?
@varunaggarwal1995
@varunaggarwal1995 7 жыл бұрын
No you cannot, but you can call a static method from static block and can initialize a static variable. you cannot access non-static content from static block
@sasmalpayal
@sasmalpayal 7 жыл бұрын
Hmm thank you
@mhlengimanqele4496
@mhlengimanqele4496 7 жыл бұрын
Exactly what I was asking myself... thank you!!!
@geethamadhurivasa5779
@geethamadhurivasa5779 4 жыл бұрын
whats the output public class Main { static String s=""; static{ s="hello world"; } public static void main(String[] args) { System.out.println(s); } static{ s=""; } static{ s="abcd"; } static { s="efgh"; } } first guess and try it in compiler
7.17 Static Import in Java
3:34
Telusko
Рет қаралды 89 М.
Survive 100 Days In Nuclear Bunker, Win $500,000
32:21
MrBeast
Рет қаралды 119 МЛН
Java methods 📞
11:05
Bro Code
Рет қаралды 126 М.
Java Reflection Explained - bɘniɒlqxƎ noiɟɔɘlʇɘЯ ɒvɒᒐ
20:07
Coding with John
Рет қаралды 177 М.
STATIC VARIABLES, STATIC METHODS & STATIC BLOCK - JAVA PROGRAMMING
22:40
Sundeep Saradhi Kanthety
Рет қаралды 232 М.
Generics in Java
14:26
Telusko
Рет қаралды 505 М.
Stop, Intel’s Already Dead! - AMD Ryzen 9600X & 9700X Review
13:47
Linus Tech Tips
Рет қаралды 1 МЛН
9.3 User Input using System.in.read() method in java
9:34
Telusko
Рет қаралды 122 М.
Java static keyword ⚡
8:18
Bro Code
Рет қаралды 110 М.
Generics In Java - Full Simple Tutorial
17:34
Coding with John
Рет қаралды 1 МЛН
Why static block executes before main () || Core Java FAQs Videos
7:05
Naresh i Technologies
Рет қаралды 48 М.
Java constructors 👷
10:37
Bro Code
Рет қаралды 172 М.
Survive 100 Days In Nuclear Bunker, Win $500,000
32:21
MrBeast
Рет қаралды 119 МЛН