---
title: "Java反射demo"
author: "jonson"
date: "2018-01-10T09:33:14Z"
tags: []
categories: ["移动开发"]
slug: "java-demo"
draft: false
lastmod: "2018-01-10T09:33:14Z"
id: 2407308
url: "https://jonson.iteye.com/blog/2407308"
---

java反射学习代码

反射调用类的静态私有成员变量的一个方法

```java

public static void clearApiService() {
    try {
      Field field = APIServiceFactory.class.getDeclaredField("mServiceInstanceMap");
      field.setAccessible(true);
      Object obj = field.get(APIServiceFactory.class);
      Method clearMethod = Map.class.getDeclaredMethod("clear");
      clearMethod.invoke(obj);

    } catch (NoSuchFieldException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }
  }

```