# Kotlin idiom for doing stuff in a ctor

**URL:** <https://discuss.kotlinlang.org/t/kotlin-idiom-for-doing-stuff-in-a-ctor/1286>\
**Category:** Uncategorized\
**Created:** [March 1, 2012, 3:43pm UTC](https://discuss.kotlinlang.org/t/kotlin-idiom-for-doing-stuff-in-a-ctor/1286 "2012-03-01T15:43:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ae6rt](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ae6rt/32/1591_2.png) [@ae6rt](https://discuss.kotlinlang.org/u/ae6rt)\
**Post date:** [March 1, 2012, 3:43pm UTC](https://discuss.kotlinlang.org/t/kotlin-idiom-for-doing-stuff-in-a-ctor/1286/1 "2012-03-01T15:43:10Z")

</div>

Hi.&nbsp;&nbsp;I am getting my feet wet with Kotln, and I'm enjoying it.

Context:&nbsp;&nbsp;IJ EAP build 114.145, plugin version kotlin-plugin-0.1.1358.zip.

I could not quite discern by reading the wiki how to do non-trivial stuff in a class constructor.&nbsp;&nbsp;e.g., what is the Kotlin equivalent of this Java class?

public class Account1 {

&nbsp;&nbsp;public final String userName;

&nbsp;&nbsp;public Account1() {  
&nbsp;&nbsp;// load properties from a classpath resource and assign a value to “userName”  
&nbsp;&nbsp;}  
}

  
I tried this, but it doesn’t work (inputstream is null):

class Account1() { &nbsp;&nbsp;var userName : String? = "" &nbsp;&nbsp;var password : String? = "" &nbsp;&nbsp;var apiKey : String? = ""

&nbsp;&nbsp;{  
&nbsp;&nbsp;val p = java.util.Properties()  
&nbsp;&nbsp;val inputStream = ClassLoader.getSystemResourceAsStream(“/mailchimp.properties”)  
&nbsp;&nbsp;p.load(inputStream)  
&nbsp;&nbsp;  
&nbsp;&nbsp;userName = p.getProperty(“username”)  
&nbsp;&nbsp;password = p.getProperty(“password”)  
&nbsp;&nbsp;apiKey = p.getProperty(“apikey”)  
&nbsp;&nbsp;}  
}

Also, how do you getClass(), so you can get a classpath resource, on in a Kotlin object instance?&nbsp;&nbsp;That static ClassLoader.getSystemResourceAsString() is not quite what I had in mind to get the classpath resource.

Thanks much.  
Mark

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [March 1, 2012, 3:56pm UTC](https://discuss.kotlinlang.org/t/kotlin-idiom-for-doing-stuff-in-a-ctor/1286/2 "2012-03-01T15:56:10Z")

</div>

> I tried this, but it doesn't work (inputstream is null):

If the input stream is null, it means that the constructor gets invoked, but you have a problem with your input stream. So, I think the constructor question doesn't need an answer. Correct me if I'm wrong.

I personally suspect this line:

> &nbsp;&nbsp;val inputStream = ClassLoader.getSystemResourceAsStream("/mailchimp.properties")

For it goes to system resourses which might not be the right place...

> Also, how do you getClass(), so you can get a classpath resource, on in a Kotlin object instance?&nbsp;&nbsp;That static ClassLoader.getSystemResourceAsString() is not quite what I had in mind to get the classpath resource.

You can say

``

```kotlin
foo.javaClass
```

to do that.

Feel free to ask more questions if you have them. Good luck!

---

<div class="post-metadata">

**Author:** ![ae6rt](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ae6rt/32/1591_2.png) [@ae6rt](https://discuss.kotlinlang.org/u/ae6rt)\
**Post date:** [March 1, 2012, 4:28pm UTC](https://discuss.kotlinlang.org/t/kotlin-idiom-for-doing-stuff-in-a-ctor/1286/3 "2012-03-01T16:28:35Z")

</div>

Thank you, kindly.&nbsp;&nbsp;That helped.

This works

class Account1() { &nbsp;&nbsp;var userName : String? = "" &nbsp;&nbsp;var password : String? = "" &nbsp;&nbsp;var apiKey : String? = "" &nbsp;&nbsp;{ &nbsp;&nbsp;val p = Properties() &nbsp;&nbsp;val clazz = javaClass &nbsp;&nbsp;val inputStream = clazz.getResourceAsStream("/mailchimp.properties") &nbsp;&nbsp;p.load(inputStream) &nbsp;&nbsp;userName = p.getProperty("username") &nbsp;&nbsp;password = p.getProperty("password") &nbsp;&nbsp;apiKey = p.getProperty("apikey") &nbsp;&nbsp;} }
