12.6 Calling Private Method in Java Class using Reflection API

Поделиться
HTML-код
  • Опубликовано: 2 янв 2025

Комментарии •

  • @aymanpatel
    @aymanpatel 4 года назад +9

    Do not use Reflection API unless you work on frameworks like Spring or libraries like Hibernate, Junit .etc.
    Why not reflection?
    1. It violates type-safety
    2. Using it to invoke private methods violates encapsulation and security guarantees.
    3. Since it is violates type-safety, it makes it exponentially harder to debug.

    • @thatoneuser8600
      @thatoneuser8600 3 года назад

      When would I need reflection in spring? I understand using it for JUnit to test private methods...

  • @Just_Bunny_life
    @Just_Bunny_life 8 лет назад +10

    Sir please make videos audible enough, so that they are easily understandable....

    • @poonampoonam305
      @poonampoonam305 3 года назад

      yeah I am also not able to hear it clearly...

  • @aiswaryasekar5526
    @aiswaryasekar5526 6 лет назад +7

    Hi Naveen,
    On using reflection, private variables can be accessed. Then how can we acheive data abstraction?
    Is there any other way to make a variable inaccessible even by reflection ?
    Kindly illustrate.

  • @ashokaj1979
    @ashokaj1979 5 лет назад +1

    Absolutely we need whole playlist on reflection

  • @singh6569
    @singh6569 8 лет назад +2

    Right way would be with generic: and we don't need to put null:---
    Class c = Class.forName("com.navin.Test");
    Test t =(Test)c.newInstance();
    Method m= c.getDeclaredMethod("show");
    m.setAccessible(true);
    m.invoke(t);

  • @francescopiazza4882
    @francescopiazza4882 5 лет назад +2

    Simply incredible!

  • @mukeshsinghdance7332
    @mukeshsinghdance7332 6 лет назад +9

    He says, reflection is not being used in real time. it used for only debugging. Spring uses so much of reflection. spring can't exist without relfection. Please give correct information.

    • @Gorky25
      @Gorky25 5 лет назад

      I am searching because of this reason (Spring) to understand why and when we use reflection.

    • @shankars4281
      @shankars4281 5 лет назад +2

      @@Gorky25 To inject dependency based on private field, spring uses reflection concept.

    • @Gorky25
      @Gorky25 5 лет назад

      @@shankars4281 Thank you!

  • @hrishikesh1593
    @hrishikesh1593 6 лет назад +2

    Hello Navin sir, could you please re-upload this video with increased volume of audio.watching java playlist best tutorials, simple but effective...thank you

  • @sunnymaurya4633
    @sunnymaurya4633 4 года назад

    The another application of reflection api is it is used in various coding platform like hackerank, codechef, to run the test cases.

  • @dineshsatpute1941
    @dineshsatpute1941 3 года назад

    Hello Naveen
    my private method accept ArrayList(Person) as argument
    so how can I write reflection for the same
    please give me answer on priority

  • @lohithdonthamshetti7944
    @lohithdonthamshetti7944 4 года назад

    Is there any particular reason creating the object by using the class function

  • @narenraj729
    @narenraj729 2 года назад

    How to invoke method if one of the parameters of the method is an interface ? Keep getting nosuchmethod exception

  • @nagarajanerode
    @nagarajanerode 3 года назад

    Good explanation

  • @chaitanyatuckley4666
    @chaitanyatuckley4666 4 года назад

    Sir what to do if the method os parameterised? How to declare it in the main function?

  • @tedtdu
    @tedtdu 8 лет назад

    Class.forName();
    c.newInstnce();
    both returns object of Class Test, right?
    What is defference then.

    • @kamesh70
      @kamesh70 5 лет назад

      Class.forName() is giving Class Class object. with that Class class object, we are creating an object for the required class.

  • @prakashtechshot9866
    @prakashtechshot9866 3 года назад

    Can we pass parameters to private method from reflex api ? Tell me how to pass

  • @mandardongare1885
    @mandardongare1885 6 лет назад +2

    Voice is not clear....

  • @soumikchoudhury2868
    @soumikchoudhury2868 3 года назад

    Wrote the same exact code , but console shows class not found exception. Any ideas? (Anyone who sees this comment)

  • @praveensirugudi9035
    @praveensirugudi9035 9 лет назад

    Hi Naveen, Could you please create a playlist for reflection concepts and share them.
    Nice Tutorials, easily understandable

  • @jatinderverma1757
    @jatinderverma1757 6 лет назад +1

    Why this concept introduced? What is the need where it can be used?

    • @aaaa-gi3vg
      @aaaa-gi3vg 5 лет назад

      for spring purpose and debugging

  • @celiafernandes9084
    @celiafernandes9084 4 года назад

    Yes

  • @karanrangra5177
    @karanrangra5177 3 года назад

    very less volume

  • @susobhandas6143
    @susobhandas6143 5 лет назад

    bad sound

  • @tanaysamanta4730
    @tanaysamanta4730 2 года назад

    //java 15.0.1 2020-10-20
    // Access Private Method Using Reflection in Java
    import java.lang.reflect.Method;
    class College {
    public static void main(String[] args) throws Exception
    {
    // Student class object
    Student e = new Student();
    // Create Method object
    Method m = Student.class.getDeclaredMethod("printName");
    // Set the accessibility as true
    m.setAccessible(true);
    m.invoke(e);
    }
    }
    // Student class declaration
    class Student {
    private void printName() { System.out.println("Tanay"); }
    }