File tree Expand file tree Collapse file tree
main/java/com/baeldung/finalkeyword
test/java/com/baeldung/finalkeyword Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .finalkeyword ;
2+
3+ public class BlackCat {
4+
5+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .finalkeyword ;
2+
3+ public class BlackDog extends Dog {
4+ // public void sound() {
5+ // }
6+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .finalkeyword ;
2+
3+ public final class Cat {
4+
5+ private int weight ;
6+
7+ public int getWeight () {
8+ return weight ;
9+ }
10+
11+ public void setWeight (int weight ) {
12+ this .weight = weight ;
13+ }
14+
15+ public void methodWithFinalArguments (final int x ) {
16+ // x=1;
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .finalkeyword ;
2+
3+ public class Dog {
4+
5+ public final void sound () {
6+ }
7+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .finalkeyword ;
2+
3+ import org .junit .Test ;
4+ import static org .junit .Assert .*;
5+
6+ public class FinalUnitTest {
7+
8+ @ Test
9+ public void whenChangedFinalClassProperties_thenChanged () {
10+ Cat cat = new Cat ();
11+ cat .setWeight (1 );
12+
13+ assertEquals (1 , cat .getWeight ());
14+
15+ }
16+
17+ @ Test
18+ public void whenFinalVariableAssign_thenOnlyOnce () {
19+ final int i ;
20+ i = 1 ;
21+ // i=2;
22+ }
23+
24+ @ Test
25+ public void whenChangedFinalReference_thenChanged () {
26+
27+ final Cat cat = new Cat ();
28+ // cat=new Cat();
29+ cat .setWeight (5 );
30+
31+ assertEquals (5 , cat .getWeight ());
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments