Friday 12 October 2012

Difference between MyClass.class and this


this :  It refers to  the current instance of the class where you are working on.

MyClass.class:  It refers to the Class instance that describes the class.

for example: Like you have a Test class
public class test{

static String  mode="EDIT";

}

You want to pass class instance as argument in another class.

If you pass Test.class as argument like below code then variables (like mode) set in your class will not get in another class.

Map maps = new HashMap();
maps.put("ARG_ID",Test.class);

If you use this in place of Test.class like below code then you will get variables of Test class in another class.

Map maps = new HashMap();
maps.put("ARG_ID",this);

No comments:

Post a Comment