12.6 Calling Private Method in Java Class using Reflection API

preview_player
Показать описание
If we want to know behavior of the class, interface of a class file, manipulation classes, fields, methods, and constructors then we can use reflection API concept of java.

Follow on Facebook:

Subscribe to our other channel:
Telusko Hindi :

Subscribe to the channel and learn Programming in easy way.

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

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.

aymanpatel
Автор

Right way would be with generic: and we don't need to put null:---

Class<?> c =
Test t =(Test)c.newInstance();
Method m= c.getDeclaredMethod("show");
m.setAccessible(true);
m.invoke(t);

singh
Автор

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.

aiswaryasekar
Автор

Absolutely we need whole playlist on reflection

ashokaj
Автор

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

Just_Bunny_life
Автор

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

chaitanyatuckley
Автор

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

lohithdonthamshetti
Автор

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

dineshsatpute
Автор

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

tedtdu
Автор

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

hrishikesh
Автор

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

narenraj
Автор

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

prakashtechshot
Автор

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

praveensirugudi
Автор

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

soumikchoudhury
Автор

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

sunnymaurya
Автор

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.

mukeshsinghdance
Автор

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

jatinderverma
Автор

//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 =

// Set the accessibility as true
m.setAccessible(true);

m.invoke(e);
}
}
// Student class declaration
class Student {
private void printName() { System.out.println("Tanay"); }
}

tanaysamanta