blob: 6de2b991efa496478e2b4f63bc1e766980029f82 [file] [log] [blame]
Clément Bérab1253f02021-02-03 10:21:33 +00001// Copyright (c) 2020, the R8 project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5package records;
6
7public class RecordWithMembers {
8
9
10 record PersonWithConstructors(String name, int age) {
11
12 public PersonWithConstructors(String name, int age) {
13 this.name = name + "X";
14 this.age = age;
15 }
16
17 public PersonWithConstructors(String name) {
18 this(name, -1);
19 }
20 }
21
22 record PersonWithMethods(String name, int age) {
23 public static void staticPrint() {
24 System.out.println("print");
25 }
26
27 @Override
28 public String toString() {
29 return name + age;
30 }
31 }
32
33 record PersonWithFields(String name, int age) {
34
35 // Extra instance fields are not allowed on records.
36 public static String globalName;
37
38 }
39
40 public static void main(String[] args) {
41 personWithConstructorTest();
42 personWithMethodsTest();
43 personWithFieldsTest();
44 }
45
46 private static void personWithConstructorTest() {
47 PersonWithConstructors bob = new PersonWithConstructors("Bob", 43);
48 System.out.println(bob.name);
49 System.out.println(bob.age);
50 System.out.println(bob.name());
51 System.out.println(bob.age());
52 PersonWithConstructors felix = new PersonWithConstructors("Felix");
53 System.out.println(felix.name);
54 System.out.println(felix.age);
55 System.out.println(felix.name());
56 System.out.println(felix.age());
57 }
58
59 private static void personWithMethodsTest() {
60 PersonWithMethods.staticPrint();
61 PersonWithMethods bob = new PersonWithMethods("Bob", 43);
62 System.out.println(bob.toString());
63 }
64
65 private static void personWithFieldsTest() {
66 PersonWithFields.globalName = "extra";
67 System.out.println(PersonWithFields.globalName);
68 }
69}