Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 1 | // Copyright (c) 2016, 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 | |
| 5 | package staticfield; |
| 6 | |
| 7 | public class StaticField { |
| 8 | |
| 9 | // Final static initialized fields, out of order, in dex these must be sorted by field idx. |
| 10 | public static final String fieldB = "B"; |
| 11 | public static final String fieldC = "C"; |
| 12 | public static final String fieldA = "A"; |
| 13 | |
| 14 | public static StaticField field = null; |
| 15 | |
| 16 | private int x; |
| 17 | |
| 18 | public StaticField(int x) { |
| 19 | this.x = x; |
| 20 | } |
| 21 | |
| 22 | @Override |
| 23 | public String toString() { |
| 24 | return "" + x; |
| 25 | } |
| 26 | |
| 27 | public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { |
| 28 | StaticField value = new StaticField(101010); |
| 29 | StaticField.field = value; |
| 30 | System.out.println(StaticField.field); |
| 31 | System.out.println(value.field); |
| 32 | |
| 33 | System.out.print(StaticField.fieldA); |
| 34 | System.out.print(StaticField.fieldB); |
| 35 | System.out.println(StaticField.fieldC); |
| 36 | |
| 37 | // Check that we can access the same static final value via the class object. |
| 38 | System.out.print(StaticField.class.getField("fieldA").get(value)); |
| 39 | System.out.print(StaticField.class.getField("fieldB").get(value)); |
| 40 | System.out.println(StaticField.class.getField("fieldC").get(value)); |
| 41 | } |
| 42 | } |