File tree Expand file tree Collapse file tree
core-java/src/main/java/com/baeldung/accessmodifiers
spring-thymeleaf/src/main/webapp/WEB-INF/views Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .accessmodifiers ;
2+
3+ public class Public {
4+ public Public () {
5+ SuperPublic .publicMethod (); // Available everywhere.
6+ SuperPublic .protectedMethod (); // Available in the same package or subclass.
7+ SuperPublic .defaultMethod (); // Available in the same package.
8+ }
9+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .accessmodifiers ;
2+
3+ public class SubClass extends SuperPublic {
4+ public SubClass () {
5+ SuperPublic .publicMethod (); // Available everywhere.
6+ SuperPublic .protectedMethod (); // Available in the same package or subclass.
7+ SuperPublic .defaultMethod (); // Available in the same package.
8+ }
9+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .accessmodifiers ;
2+
3+ //Only public or default access modifiers are permitted
4+ public class SuperPublic {
5+ // Always available from anywhere
6+ static public void publicMethod () {
7+ System .out .println (SuperPublic .class .getName () + " publicMethod()" );
8+ }
9+
10+ // Available within the same package
11+ static void defaultMethod () {
12+ System .out .println (SuperPublic .class .getName () + " defaultMethod()" );
13+ }
14+
15+ // Available within the same package and subclasses
16+ static protected void protectedMethod () {
17+ System .out .println (SuperPublic .class .getName () + " protectedMethod()" );
18+ }
19+
20+ // Available within the same class only
21+ static private void privateMethod () {
22+ System .out .println (SuperPublic .class .getName () + " privateMethod()" );
23+ }
24+
25+ // Method in the same class = has access to all members within the same class
26+ private void anotherPrivateMethod () {
27+ privateMethod ();
28+ defaultMethod ();
29+ protectedMethod ();
30+ publicMethod (); // Available in the same class only.
31+ }
32+ }
33+
34+ // Only public or default access modifiers are permitted
35+ class SuperDefault {
36+ public void publicMethod () {
37+ System .out .println (this .getClass ().getName () + " publicMethod()" );
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .accessmodifiers .another ;
2+
3+ import com .baeldung .accessmodifiers .SuperPublic ;
4+
5+ public class AnotherPublic {
6+ public AnotherPublic () {
7+ SuperPublic .publicMethod (); // Available everywhere.
8+ }
9+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .accessmodifiers .another ;
2+
3+ import com .baeldung .accessmodifiers .SuperPublic ;
4+
5+ public class AnotherSubClass extends SuperPublic {
6+ public AnotherSubClass () {
7+ SuperPublic .publicMethod (); // Available everywhere.
8+ SuperPublic .protectedMethod (); // Available in subclass. Let's note different package.
9+ }
10+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .accessmodifiers .another ;
2+
3+ import com .baeldung .accessmodifiers .SuperPublic ;
4+
5+ public class AnotherSuperPublic {
6+ public AnotherSuperPublic () {
7+ SuperPublic .publicMethod (); // Available everywhere. Let's note different package.
8+ }
9+ }
Original file line number Diff line number Diff line change @@ -32,7 +32,9 @@ <h1>Add Student</h1>
3232 </ tr >
3333 < tr >
3434 < td > < label th:text ="#{msg.percent} " /> </ td >
35- < td > < input type ="text " th:field ="*{percentage} " /> </ td >
35+ < td > < select id ="percentage " name ="percentage ">
36+ < option th:each ="i : ${#numbers.sequence(0, 100)} " th:value ="${i} " th:text ="${i} " th:selected ="${i==75} "> </ option >
37+ </ select > </ td >
3638 </ tr >
3739 < tr >
3840 < td > < input type ="submit " value ="Submit " /> </ td >
You can’t perform that action at this time.
0 commit comments