Understanding Java Reflection: Concepts, Usage, and Performance Considerations
This article explains Java's reflection mechanism, its core classes, practical uses such as accessing private members and dynamic loading, demonstrates code examples, discusses performance, security and compatibility issues, and provides guidance for Android developers.
Reflection is a mechanism that allows dynamic access and modification of class members at runtime, including private fields and methods.
The core classes involved are java.lang.Class, java.lang.reflect.Constructor, java.lang.reflect.Method, and java.lang.reflect.Field.
Typical uses include bypassing access restrictions, implementing custom annotations, dynamic loading of third‑party JARs (useful in Android to avoid the 64K method limit), and on‑demand class loading to reduce APK size and startup time.
The working principle: compiled .class files are loaded by a ClassLoader, creating Class objects that hold metadata; reflection uses the four core classes to inspect and manipulate this metadata.
Code Example
Below are sample classes used to demonstrate reflection:
package com.eebbk.reflectdemo;
public class Person {
String mName;
String mSex;
public int mAge;
public Person(String aName, String aSex, int aAge) {
mName = aName;
mSex = aSex;
mAge = aAge;
}
public int getmAge() {
return mAge;
}
public void setmAge(int mAge) {
this.mAge = mAge;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
public String getmSex() {
return mSex;
}
public void setmSex(String mSex) {
this.mSex = mSex;
}
private String getDescription() {
return "黄种人";
}
} package com.eebbk.reflectdemo;
public interface ICompany {
String getCompany();
} package com.eebbk.reflectdemo;
public class ProgramMonkey extends Person implements ICompany {
String mLanguage = "C#";
String mCompany = "BBK";
public ProgramMonkey(String aName, String aSex, int aAge) {
super(aName, aSex, aAge);
}
public ProgramMonkey(String language, String company, String aName, String aSex, int aAge) {
super(aName, aSex, aAge);
mLanguage = language;
mCompany = company;
}
public String getmLanguage() {
return mLanguage;
}
public void setmLanguage(String mLanguage) {
this.mLanguage = mLanguage;
}
private int getSalaryPerMonth() {
return 12306;
}
@Override
public String getCompany() {
return mCompany;
}
} public class ReflectActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reflect_layout);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.getClassObjectBtnId:{
getClassObject();
}
break;
case R.id.getMethodInfoBtnId:{
getMethodInfo();
}
break;
case R.id.getFieldInfoBtnId:{
getFieldInfo();
}
break;
case R.id.getSuperClassInfoBtnId:{
getSuperClass();
}
break;
case R.id.getInterfaceInfoBtnId:{
getInterfaces();
}
break;
case R.id.compareMethodAndFieldBtnId:{
compareCallMethodAndField();
}
break;
default:{
}
break;
}
}
private void getClassObject() {
Class<?> classObject = null;
classObject = getClassObject_1();
LogE("classObject_1 name : " + classObject.getName());
classObject = getClassObject_2();
LogE("classObject_2 name : " + classObject.getName());
classObject = getClassObject_3();
LogE("classObject_3 name : " + classObject.getName());
}
// ... other helper methods omitted for brevity ...
}The demo shows three ways to obtain class information, retrieve methods and fields of the current class and its superclasses, access interfaces, and compare performance between reflective method invocation and direct field access.
Advantages
High flexibility: can operate on classes regardless of access modifiers.
Disadvantages
Performance overhead: reflective calls are slower than direct calls.
Security risk: can break encapsulation.
Compatibility issues: API changes may cause reflection failures.
Key takeaways: reflection is powerful but should be used sparingly, avoid obfuscation of reflective classes, and be aware of performance, security, and version‑compatibility concerns, especially on Android.
References include the official Java Reflection tutorial and several Chinese technical blogs.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
