forked from Hesenius/SafariJava17CertStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package enumstuff; | ||
|
||
enum Day { | ||
MONDAY(1), TUESDAY{}; | ||
// enum constructors default to private and must be private | ||
/*public*/ Day(int d) {} | ||
/*public*/ Day() {} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "I'm an enum value is " + name(); | ||
} | ||
// public String name() {} //NOPE, name is final :) | ||
} | ||
|
||
public class Ex1 { | ||
public static void main(String[] args) { | ||
System.out.println(Day.MONDAY); | ||
System.out.println(Day.MONDAY.getClass()); | ||
System.out.println(Day.TUESDAY.getClass()); | ||
|
||
Day m = Day.valueOf("MONDAY"); | ||
|
||
for (var d : Day.values()) { | ||
System.out.println("> " + d); | ||
System.out.println("ordinal is " + d.ordinal()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package innerclasses; | ||
|
||
class Outer1 { | ||
class Inner1 { | ||
// int x = 100; | ||
private int y = 100; | ||
// line n1 | ||
Inner1 makeOne() { // this would work, Outer1.this is implicit | ||
// static Inner1 makeOne() { // fails, no enclosing instance | ||
// return new Inner1(); // fails with static context | ||
// return new Outer1().new Inner1(); // OK with static or instance context | ||
return /*Outer1.this.*/new Inner1(); // OK with static or instance context | ||
} | ||
void showX() { | ||
System.out.println("x is " + /*Outer1.this.*/x); | ||
} | ||
} | ||
class Inner2 { | ||
void showAnotherY(Inner1 another) { | ||
System.out.println("y is " + another.y); | ||
} | ||
} | ||
private int x = 99; | ||
// line n2 | ||
// static Inner1 makeOne() { return new Inner1(); } | ||
// void showY() { | ||
// System.out.println("y is " + y); // NOPE, which Inner?? | ||
// } | ||
void showAnotherY(Inner1 another) { | ||
System.out.println("y is " + another.y); | ||
} | ||
} | ||
|
||
// void showX() { sop("x is " + x); } | ||
// void showY() { sop("y is " + y); } | ||
// void showAnotherY(Inner1 another) { sop("y is " + another.y); } | ||
//static Inner1 makeOne() { return new Inner1(); } | ||
public class Access { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package innerclasses; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public class AnonymousEx { | ||
public void doStuff() { | ||
int len = 3; | ||
// parent type can be class, abstract class, or interface | ||
// if parent is a class (incl abstract) we can pass | ||
// constructor args | ||
// Lifetime (also for lambdas) is governed by reachability and GC | ||
// But, lifetime of len (above) would normally be method invocation | ||
Predicate<String> ps = new Predicate<String>() { | ||
// declare arbitrary fields, methods, etc. | ||
|
||
private int x = 99; | ||
|
||
@Override | ||
public String toString() { | ||
return "Hah, I'm a anonymous"; | ||
} | ||
|
||
@Override | ||
public boolean test(String s) { | ||
// len ++; | ||
return s.length() > len; // line n1 | ||
} | ||
}; | ||
// line n2 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package recordstuff; | ||
|
||
// purpose is "immutable" data carrier (named tuples? with extras) | ||
// accessor methods created for each "component" | ||
// has name of the component with empty parens, throws no checked exceptions | ||
// has the type of the component (if varags then array) | ||
// components create final fields with the same name | ||
// components cannot have the name of an Object method (e.g. wait, toString :) | ||
record Client(String name, int creditLimit) { // these two are "components" | ||
// line n1 | ||
// not permitted to add any instance fields | ||
// private final int x = 99; | ||
// are permitted static fields, don't even have to be final | ||
private static int x; | ||
// are permitted instance, "overriding", and static methods | ||
|
||
@Override // Not really "overriding" -- it's replacing | ||
public String toString() { | ||
// return super.toString(); // NOPE, there is no super... | ||
return "I'm a client with credit " + creditLimit; | ||
} | ||
} | ||
|
||
public class Example { | ||
public static void main(String[] args) { | ||
Client c1 = new Client("Fred", 99); | ||
Client c2 = new Client("Fred", 99); | ||
Client c3 = new Client("Jim", 99); | ||
System.out.println(c1); // toString :) | ||
System.out.println(c1.name()); | ||
System.out.println("equality"); | ||
System.out.println(c1.equals(c2)); | ||
System.out.println(c1.equals(c3)); | ||
|
||
System.out.println("hashCodes"); | ||
System.out.println(c1.hashCode()); | ||
System.out.println(c2.hashCode()); | ||
System.out.println(c3.hashCode()); | ||
|
||
|
||
} | ||
} |