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.
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.
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);
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.
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
//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"); } }
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.
When would I need reflection in spring? I understand using it for JUnit to test private methods...
Sir please make videos audible enough, so that they are easily understandable....
yeah I am also not able to hear it clearly...
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.
Good question
Absolutely we need whole playlist on reflection
No you don't
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);
Simply incredible!
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.
I am searching because of this reason (Spring) to understand why and when we use reflection.
@@Gorky25 To inject dependency based on private field, spring uses reflection concept.
@@shankars4281 Thank you!
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
use fucking earphones
@@aaaa-gi3vg Bitch, you learn respect first!
The another application of reflection api is it is used in various coding platform like hackerank, codechef, to run the test cases.
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
Is there any particular reason creating the object by using the class function
How to invoke method if one of the parameters of the method is an interface ? Keep getting nosuchmethod exception
Good explanation
Sir what to do if the method os parameterised? How to declare it in the main function?
Class.forName();
c.newInstnce();
both returns object of Class Test, right?
What is defference then.
Class.forName() is giving Class Class object. with that Class class object, we are creating an object for the required class.
Can we pass parameters to private method from reflex api ? Tell me how to pass
Voice is not clear....
Wrote the same exact code , but console shows class not found exception. Any ideas? (Anyone who sees this comment)
Hi Naveen, Could you please create a playlist for reflection concepts and share them.
Nice Tutorials, easily understandable
Why this concept introduced? What is the need where it can be used?
for spring purpose and debugging
Yes
very less volume
bad sound
//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"); }
}