File tree Expand file tree Collapse file tree
java/com/baeldung/constructor
kotlin/com/baeldung/constructor Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com.baeldung.constructor
2+
3+ class Car {
4+ val id: String
5+ val type: String
6+
7+ constructor (id: String , type: String ) {
8+ this .id = id
9+ this .type = type
10+ }
11+
12+ }
13+
14+ fun main (args : Array <String >) {
15+ val car = Car (" 1" , " sport" )
16+ val s= Car (" 2" , " suv" )
17+ }
Original file line number Diff line number Diff line change 1+ package com.baeldung.constructor
2+
3+ class Employee (name : String , val salary : Int ): Person(name)
Original file line number Diff line number Diff line change 1+ package com .baeldung .constructor ;
2+
3+ class PersonJava {
4+ final String name ;
5+ final String surname ;
6+ final Integer age ;
7+
8+ public PersonJava (String name , String surname ) {
9+ this .name = name ;
10+ this .surname = surname ;
11+ this .age = null ;
12+ }
13+
14+ public PersonJava (String name , String surname , Integer age ) {
15+ this .name = name ;
16+ this .surname = surname ;
17+ this .age = age ;
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package com.baeldung.constructor
2+
3+ open class Person (
4+ val name : String ,
5+ val age : Int? = null
6+ ) {
7+ val upperCaseName: String = name.toUpperCase()
8+
9+ init {
10+ println (" Hello, I'm $name " )
11+
12+ if (age != null && age < 0 ) {
13+ throw IllegalArgumentException (" Age cannot be less than zero!" )
14+ }
15+ }
16+
17+ init {
18+ println (" upperCaseName is $upperCaseName " )
19+ }
20+
21+ }
22+
23+ fun main (args : Array <String >) {
24+ val person = Person (" John" )
25+ val personWithAge = Person (" John" , 22 )
26+ }
You can’t perform that action at this time.
0 commit comments