1

I have a code in java where i have initialised the String like this way

String str = new String("");

but when i have converted the code to kotlin the same intilization is in the below way

   private val mEmailApi : String("")

but it is giving me a error getter and setter required can you guys please let me know the way to initialise the String in Kotlin

Thanks In Advance

5
  • 2
    val mEmailApi = "" Commented Dec 26, 2017 at 5:09
  • thanks @ramineftekhari Commented Dec 26, 2017 at 5:32
  • 1
    Was this code generated by the J2K code converter? If so, please file an issue on the kotlin bug tracker Commented Dec 26, 2017 at 6:27
  • 2
    Why would you ever write the original Java code instead of String str = "";? Commented Dec 26, 2017 at 8:42
  • 1
    If you are creating a string literal (e.g. you don't need create a string from an existing byte[] or char[]), do not use a constructor to create the String and instead use the literal syntax instead. Using String literals will prevent unnecessary allocation from occurring by allowing the String to be interned (see docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5) Commented Dec 26, 2017 at 11:47

2 Answers 2

4

private var mString:String?=null

lateinit private var mString:String

Sign up to request clarification or add additional context in comments.

Comments

3

So private val mEmailApi : String("") you just define type of your field mEmailApi. But calling constructor during type declaration impossible.

And so: private val mEmailApi = "" you initialize it.

If you initialize your filed during to define, you need not to define type obviously.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.