# Reflection and Class object instances

**URL:** <https://discuss.kotlinlang.org/t/reflection-and-class-object-instances/1120>\
**Category:** Uncategorized\
**Created:** [July 18, 2012, 3:19pm UTC](https://discuss.kotlinlang.org/t/reflection-and-class-object-instances/1120 "2012-07-18T15:19:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![programingjd](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/programingjd/32/1526_2.png) [@programingjd](https://discuss.kotlinlang.org/u/programingjd)\
**Post date:** [July 18, 2012, 3:19pm UTC](https://discuss.kotlinlang.org/t/reflection-and-class-object-instances/1120/1 "2012-07-18T15:19:42Z")

</div>

I am wondering how to use java reflection with kotlin.

Let’s consider something like this:

`final Format format = new DecimalFormat(“#.00”);`  
final Number number = 1.2;

_// Format.format(Object) using reflection (maybe those objects are on a different class loader)_  
`final String formatted = (String)format.getClass().getMethod(“format”, Object.class).invoke(format, number); `

So far I have this:

` `  
`val format: Format = DecimalFormat(“#.00”)`  
` `  
`val number: Number = 1.2`

`val formatted = format.javaClass.getMethod(“format”, <Missing piece>)?.invoke(format, number) as String`

As you can see, I am missing a piece: How to translate the `Object.class` bit. I can’t use `number.javaClass`, because it won’t give me `Object.class`.

Is there a syntax for `Object.class`?

I ran into the problem before with android intents:

` `  
`final Intent intent = new Intent(activity, OtherActivity.class);`  
``

`activity.startActivity(intent);`

In this particular case, I cannot get an instance of `OtherActivity` to call `.javaClass` on.

The workaround is to create the Intent like this:

` `  
`final Intent intent = new Intent(null);`  
` `

`intent.setComponent(activity.getPackageName(), className);`

This is only possible because this `setComponent` method exists and is public.

---

<div class="post-metadata">

**Author:** ![jstrachan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstrachan/32/1559_2.png) [@jstrachan](https://discuss.kotlinlang.org/u/jstrachan)\
**Post date:** [July 18, 2012, 3:23pm UTC](https://discuss.kotlinlang.org/t/reflection-and-class-object-instances/1120/2 "2012-07-18T15:23:53Z")

</div>

try: javaClass\<Object\>() to get the Object.class

---

<div class="post-metadata">

**Author:** ![programingjd](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/programingjd/32/1526_2.png) [@programingjd](https://discuss.kotlinlang.org/u/programingjd)\
**Post date:** [July 18, 2012, 3:30pm UTC](https://discuss.kotlinlang.org/t/reflection-and-class-object-instances/1120/3 "2012-07-18T15:30:36Z")

</div>

Great, thanks.
