#what will happen if I don't use `val` in constructor ?

1 messages · Page 1 of 1 (latest)

trail rampart
#

I found that I can use constructor like

class Hello(name: String)

and

class World(val name: String)

what is the difference ?

steel garnetBOT
#

<@&1008423204219531294> please have a look, thanks.

steel garnetBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

tidal prawn
#

with latter, it will make name a property

#

in java terms: a field

#

with former, it is only a parameter (available during construction)

#

so the java equivalent, if that helps u:

#
class Hello {
  ...
  public Hello(String name) {
    ...
  }
}

vs

class Hello {
  private final String name;

  public Hello(String name) {
    this.name = name;
    ...
  }

  public String getName() { return name; }

  ...
}
trail rampart
#

thank you, I got it