亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Java Reflection API [一]

標簽:
Java

Java Reflection is a process of examining or modifying the run time behavior of a class at run time.

The java.lang.Class class provides many methods that can be used to get metadata, examine and change the run time behavior of a class.

The java.lang and java.lang.reflect packages provide classes for java reflection.

Where it is used
The Reflection API is mainly used in:

  • IDE (Integrated Development Environment) e.g. Eclipse, MyEclipse, NetBeans etc.
  • Debugger
  • Test Tools etc.

java.lang.Class class

The java.lang.Class class performs mainly two tasks:

  • provides methods to get the metadata of a class at run time.
  • provides methods to examine and change the run time behavior of a class.

How to get the object of Class class?

There are 3 ways to get the instance of Class class. They are as follows:

  • forName() method of Class class
  • getClass() method of Object class
  • the .class syntax
    1) forName() method of Class class

  • is used to load the class dynamically.
  • returns the instance of Class class.
  • It should be used if you know the fully qualified name of class.This cannot be used for primitive types.

Let's see the simple example of forName() method.

class Simple{}  

class Test{  
 public static void main(String args[]){  
   Class c=Class.forName("Simple");  
   System.out.println(c.getName());  
 }  
}  

2) getClass() method of Object class

It returns the instance of Class class. It should be used if you know the type. Moreover, it can be used with primitives.

class Simple{}  

class Test{  
  void printName(Object obj){  
    Class c=obj.getClass();    
    System.out.println(c.getName());  
  }  
  public static void main(String args[]){  
    Simple s=new Simple();  

    Test t=new Test();  
    t.printName(s);  
  }  
} 

3) The .class syntax

If a type is available but there is no instance then it is possible to obtain a Class by appending ".class" to the name of the type.It can be used for primitive data type also.

class Test{  
  public static void main(String args[]){  
    Class c = boolean.class;   
    System.out.println(c.getName());  

    Class c2 = Test.class;   
    System.out.println(c2.getName());  
  }  
}  

Determining the class object

Following methods of Class class is used to determine the class object:

  1. public boolean isInterface(): determines if the specified Class object represents an interface type.
  2. public boolean isArray(): determines if this Class object represents an array class.
  3. public boolean isPrimitive(): determines if the specified Class object represents a primitive type.
    Let's see the simple example of reflection api to determine the object type.
class Simple{}  
interface My{}  

class Test{  
 public static void main(String args[]){  
  try{  
   Class c=Class.forName("Simple");  
   System.out.println(c.isInterface());  

   Class c2=Class.forName("My");  
   System.out.println(c2.isInterface());  

  }catch(Exception e){System.out.println(e);}  

 }  
}  

source:
https://www.javatpoint.com/java-reflection

點擊查看更多內容
2人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
JAVA開發工程師
手記
粉絲
2
獲贊與收藏
70

關注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消