#39 Static Block in java

Поділитися
Вставка
  • Опубліковано 1 лют 2025

КОМЕНТАРІ • 55

  • @ahmadfarhadzammani5261
    @ahmadfarhadzammani5261 Рік тому +28

    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.

  • @fascinatingtop1015
    @fascinatingtop1015 5 місяців тому +4

    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.

    • @nitingiri7773
      @nitingiri7773 Місяць тому

      Yes, he knows everything. He is teaching on youtube more 10 years.

  • @tpttime4897
    @tpttime4897 2 роки тому +14

    Underrated channel

  • @ajayghode3602
    @ajayghode3602 Місяць тому +1

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

  • @surabhigupta9814
    @surabhigupta9814 Рік тому +5

    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.

    • @danushipathirana40
      @danushipathirana40 10 місяців тому +2

      thank you, you saved me

    • @yourwishof.11.11
      @yourwishof.11.11 3 місяці тому

      Hi @surabhigupta9814 can you please write it down the syntax so that I can get more clarity on it, cause I am facing the exception in Thread. Thank You!

    • @aarthimuthaiya6723
      @aarthimuthaiya6723 Місяць тому

      @@yourwishof.11.11 it should be package name.classname example: Class.forName("hello.Mobile"); --> my package name is hello. So by giving like this it resolves error and we get output as : in static block

  • @jatinsharma3792
    @jatinsharma3792 Рік тому +26

    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.

  • @MicronOfMotion
    @MicronOfMotion 5 місяців тому +8

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

  • @chinmayrath8494
    @chinmayrath8494 Рік тому

    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

  • @ajayghode3602
    @ajayghode3602 Місяць тому

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

  • @jellyjollyjelly9513
    @jellyjollyjelly9513 Рік тому

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

  • @sarikasona4333
    @sarikasona4333 2 роки тому +1

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

  • @padmini3268
    @padmini3268 Рік тому +2

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

  • @sakthipriya8653
    @sakthipriya8653 Рік тому +7

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

    • @obviouscreed
      @obviouscreed 10 місяців тому +1

      There are several scenarios where using a static block for initializing a static variable with additional logic or computations may be necessary or beneficial:
      1. Initialization based on environment variables: You might need to initialize a static variable based on environment-specific configurations or system properties. In such cases, you may need to read environment variables or system properties and perform validation or transformation before assigning the value to the static variable.
      2. Initialization requiring resource loading: If your static variable needs to be initialized with data from an external resource such as a file, database, or network resource, you may use a static block to load and process the resource data before assigning it to the static variable.
      3. Dynamic initialization: Sometimes, the initialization value of a static variable may depend on runtime conditions or dynamic calculations. For example, initializing a static variable based on the current timestamp, random number generation, or other dynamic computations might require using a static block.
      4. Complex initialization logic: If the initialization logic for a static variable is complex and involves multiple steps or conditions, using a static block can help organize the code and improve readability by separating the initialization logic from the variable declaration.

    • @vishnuunkili2724
      @vishnuunkili2724 8 місяців тому

      But it's not possible.

  • @hameedferoz8804
    @hameedferoz8804 Рік тому

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

  • @ameyajangam4305
    @ameyajangam4305 9 місяців тому

    these videos are cleannnnnn 🔥

  • @sriramkannan8260
    @sriramkannan8260 4 місяці тому

    @teusko it did call static block without creation of obejct

  • @subhparekh4707
    @subhparekh4707 Рік тому +1

    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.

  • @shivarajme
    @shivarajme Місяць тому

    good one!

  • @abheist_
    @abheist_ 6 місяців тому

    Does constructor and static does not need data type ?

  • @gedelasivakrishna
    @gedelasivakrishna 21 день тому

    Thankyou !!

  • @md.ikramulislam9708
    @md.ikramulislam9708 4 місяці тому

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

  • @gowrigandikota8521
    @gowrigandikota8521 Рік тому +1

    Why is static block needed, when we can directly initialize static variables in class itself?

    • @abishekathangaselvam6673
      @abishekathangaselvam6673 Рік тому

      If u want to use static variable as constructor u need to create static variables inside static block I think so

    • @taiwo_teninlanimi
      @taiwo_teninlanimi Рік тому

      Remember that its not all concepts that apply at every situation. Yours might not need static as a constructor but some other cases may

    • @obviouscreed
      @obviouscreed 10 місяців тому

      There are several scenarios where using a static block for initializing a static variable with additional logic or computations may be necessary or beneficial:
      1. Initialization based on environment variables: You might need to initialize a static variable based on environment-specific configurations or system properties. In such cases, you may need to read environment variables or system properties and perform validation or transformation before assigning the value to the static variable.
      2. Initialization requiring resource loading: If your static variable needs to be initialized with data from an external resource such as a file, database, or network resource, you may use a static block to load and process the resource data before assigning it to the static variable.
      3. Dynamic initialization: Sometimes, the initialization value of a static variable may depend on runtime conditions or dynamic calculations. For example, initializing a static variable based on the current timestamp, random number generation, or other dynamic computations might require using a static block.
      4. Complex initialization logic: If the initialization logic for a static variable is complex and involves multiple steps or conditions, using a static block can help organize the code and improve readability by separating the initialization logic from the variable declaration.

  • @ZeePatel-f3j
    @ZeePatel-f3j Рік тому

    Okay sir found it the video number 40 should be 39 and 39 shoud be 40. Thanks

  • @SanataniRishu02
    @SanataniRishu02 6 місяців тому +1

    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.

    • @SanataniRishu02
      @SanataniRishu02 6 місяців тому

      Ok so basically static block will be first to get executed within a class irrespective of its position on which line it is.

    • @demoaccount9682
      @demoaccount9682 3 місяці тому

      same here

  • @agilan1546
    @agilan1546 11 місяців тому

    In which video we learnt about constructors?

  • @ZeePatel-f3j
    @ZeePatel-f3j Рік тому

    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

  • @siddivinayak428
    @siddivinayak428 4 дні тому

    this is very confusing sir ,i didnt understand properly.
    and the code is not executing after coppying the syntax and the concept
    class Mobile
    {
    static String brand;
    String name;
    int price;
    static
    {
    brand = "apple";
    System.out.println("in static block");
    }
    public Mobile()
    {
    name = "15promax";
    price = 189999;
    System.out.println("non static block");
    }
    public void show()
    {
    System.out.println(brand+":"+name+":"+price);
    }
    }
    public class lecture39 {
    Mobile obj = new Mobile();
    obj.name = "15promax";
    obj.price = 189999;
    Mobile.brand = "apple";
    Mobile obj1 = new Mobile();
    }

  • @kesagaurav8100
    @kesagaurav8100 2 місяці тому

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

  • @balajib5126
    @balajib5126 6 місяців тому

    Sir but I can load static method without creating any object..

  • @deshdeepaksingh1182
    @deshdeepaksingh1182 9 місяців тому +1

    Does anyone know what tool he is using for writing?

  • @RameshMaity90
    @RameshMaity90 11 місяців тому +1

    But without create object still Static block is running ..Here see this code
    public class StaticHack {
    static
    {
    System.out.println("Static block");
    }
    StaticHack(){
    System.out.println("Constructor");
    }
    public static void main(String[] args) {
    // StaticHack obj=new StaticHack();
    }
    }

    • @obviouscreed
      @obviouscreed 10 місяців тому +2

      Because your static block is present in class StaticHack which has the main method & this class will obviously be loaded by the class loader and hence the execution of static block.

  • @HighPingMonk
    @HighPingMonk Рік тому +2

    Respect ++

  • @puruagni1927
    @puruagni1927 9 місяців тому

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

  • @MaheshKumar-wk7ub
    @MaheshKumar-wk7ub Рік тому

    Discord link is not working.

  • @victorniner
    @victorniner 17 днів тому

    this video didnt make sense to me

  • @Trendinghook
    @Trendinghook Рік тому +1

    Hey hi Naveen,
    I have a small doubt without creating an object i call my static block it's working
    Example
    In my class have
    Class staticConcept{
    Static{
    S.o.p("hello");
    }
    Static{
    S.o.p("hi");
    }
    P s v m (String args[])
    {
    S.o.p("world");
    }
    It's working without creating an object

    • @jagannathr724
      @jagannathr724 Рік тому

      yes It will work at the time of class loading

    • @usmanshaikh1135
      @usmanshaikh1135 Рік тому

      because all your static blocks and main method are inside the same class

  • @nisal_pubudu
    @nisal_pubudu Рік тому

    Thank you