Classes.
CLASS declarations define new reference types and describe how they are implemented. A top level class is a class that is not a nested class.
A nested class is any class whose declaration occurs within the body of another class or interface.
A named class may be declared abstract and must be declared abstract if it is incompletely implemented; such a class cannot be instantiated, but can be extended by subclasses. . A class may be declared final in which case it cannot have subclasses.
If a class is declared public, then it can be referred to from other packages.
Each class except Object is an extension of (subclass) a single existing class and may implement interfaces. Classes may be generic, that is, they may declare type variables whose bindings may differ among different instances of the class.
Class declaration:
A class declaration defines new named reference.
There are two kinds of class declarations: normal class declarations and enum declarations.
Class modifier.
The access modifier public pertains only to top level classes and member classes, not to local classes or anonymous classes.
The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration.The modifier static pertains only to member classes, not to top level or local or anonymous classes.
Abstract classes.
An abstract class is a class that is incomplete, or to be considered incomplete. It is a compile-time error if an attempt is made to create an instance of an abstract class using a class instance creation expression
final Classes
A class can be declared final if its definition is complete and no subclasses are
desired or required.
strictfp Classes
The effect of the strictfp modifier is to make all float or double expressions within the class declaration (including within variable initializers, instance initializers, static initializers, and constructors) be explicitly FP-strict. This implies that all methods declared in the class, and all nested types declared in the class, are implicitly strictfp.
ENUM
import java.util.List;
import java.util.ArrayList;
class Card implements Comparable<Card>, java.io.Serializable {
public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,JACK, QUEEN, KING, ACE }
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
private final Rank rank;
private final Suit suit;
public Rank rank() {
return rank;
}
public Suit suit() {
return suit;
}
private Card(Rank rank, Suit suit) {
if (rank == null || suit == null)
throw new NullPointerException(rank + ", " + suit);
this.rank = rank;
this.suit = suit;
}
public String toString() {
return rank + " of " + suit;
}
// Primary sort on suit, secondary sort on rank
public int compareTo(Card c) {
int suitCompare = suit.compareTo(c.suit);
return (suitCompare != 0 ? suitCompare : rank.compareTo(c.rank));
}
private static final List<Card> prototypeDeck = new ArrayList<Card>(52);
CLASS declarations define new reference types and describe how they are implemented. A top level class is a class that is not a nested class.
A nested class is any class whose declaration occurs within the body of another class or interface.
A named class may be declared abstract and must be declared abstract if it is incompletely implemented; such a class cannot be instantiated, but can be extended by subclasses. . A class may be declared final in which case it cannot have subclasses.
If a class is declared public, then it can be referred to from other packages.
Each class except Object is an extension of (subclass) a single existing class and may implement interfaces. Classes may be generic, that is, they may declare type variables whose bindings may differ among different instances of the class.
Class declaration:
A class declaration defines new named reference.
There are two kinds of class declarations: normal class declarations and enum declarations.
Class modifier.
The access modifier public pertains only to top level classes and member classes, not to local classes or anonymous classes.
The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration.The modifier static pertains only to member classes, not to top level or local or anonymous classes.
Abstract classes.
An abstract class is a class that is incomplete, or to be considered incomplete. It is a compile-time error if an attempt is made to create an instance of an abstract class using a class instance creation expression
final Classes
A class can be declared final if its definition is complete and no subclasses are
desired or required.
strictfp Classes
The effect of the strictfp modifier is to make all float or double expressions within the class declaration (including within variable initializers, instance initializers, static initializers, and constructors) be explicitly FP-strict. This implies that all methods declared in the class, and all nested types declared in the class, are implicitly strictfp.
Generic classes and type parameter.
A class is generic if it declares one or more type variables. These type variables are known as the type parameters of the class. The type parameter section follows the class name and is delimited by angle brackets.
Mutually Recursive Type Variable Bounds.
interface ConvertibleTo<T> {
T convert();
}
class ReprChange<T extends ConvertibleTo<S>, S extends ConvertibleTo<T>> {
T t;
void set(S s) {
t = s.convert();
}
S get() {
return t.convert();
}
}
Nested Generic Classes.
class Seq<T> {
T head;
Seq<T> tail;
Seq() {
this(null, null);
}
Seq(T head, Seq<T> tail) {
this.head = head;
this.tail = tail;
}
boolean isEmpty() {
return tail == null;
}
class Zipper<S> {
Seq<Pair<T,S>> zip(Seq<S> that) {
if (isEmpty() || that.isEmpty()) {
return new Seq<Pair<T,S>>();
}
else {
Seq<T>.Zipper<S> tailZipper = tail.new Zipper<S>();
return new Seq<Pair<T,S>>( new Pair<T,S>(head, that.head), tailZipper.zip(that.tail));
}
}
}
}
class Pair<T, S> {
T fst; S snd;
Pair(T f, S s) {
fst = f; snd = s;
}
}
class Test {
public static void main(String[] args) {
Seq<String> strs = new Seq<String>("a",new Seq<String>("b", new Seq<String>()));
System.out.println(strs.toString());
Seq<Number> nums = new Seq<Number>(new Integer(1), new Seq<Number>(new Double(1.5), new Seq<Number>()));
Seq<String>.Zipper<Number> zipper = strs.new Zipper<Number>();
Seq<Pair<String,Number>> combined = zipper.zip(nums);
}
}
Inner class and enclosing instances.
class HasStatic {
static int j = 100;
}
class Outer {
class Inner extends HasStatic {
static final int x = 3; // OK: constant variable
//static int y = 4; // Compile-time error: an inner class
}
static class NestedButNotInner{
static int z = 5; // OK: not an inner class
}
interface NeverInner {} // Interfaces are never inner
}
Deeply nested class.
class WithDeepNesting {
boolean toBe;
WithDeepNesting(boolean b) {
toBe = b;
}
class Nested {
boolean theQuestion;
class DeeplyNested {
DeeplyNested(){
theQuestion = toBe || !toBe;
}
}
}
}
Super class and subclasses.
Direct superclassesclass Point { int x, y; }
final class ColoredPoint extends Point { int color; }
class Colored3DPoint extends ColoredPoint { int z; } // error and subclasses.
Superclasses and subclasses.
class Point { int x, y; }
class ColoredPoint extends Point { int color; }
final class Colored3dPoint extends ColoredPoint { int z; }
Superinterfaces.
Illegal Superinterfaces
class Redundant implements java.lang.Cloneable, Cloneable {
int x;
}
-------------- ------------------------ -------------
interface Colorable {
void setColor(int color);
int getColor();
}
enum Finish { MATTE, GLOSSY }
interface Paintable extends Colorable {
void setFinish(Finish finish);
Finish getFinish();
}
class Point {
int x, y;
}
class ColoredPoint extends Point implements Colorable {
int color;
public void setColor(int color) {
this.color = color;
}
public int getColor() {
return color;
}
}
class PaintedPoint extends ColoredPoint implements Paintable {
Finish finish;
public void setFinish(Finish finish) {
this.finish = finish;
}
public Finish getFinish() {
return finish;
}
}
Class member.
class Point {
int x, y;
/*private Point() {
reset();
}*/
Point() {
reset();
}
Point(int x, int y) {
this.x = x; this.y = y;
}
private void reset() {
this.x = 0; this.y = 0;
}
}
class ColoredPoint extends Point {
int color;
void clear() {
//reset();
} // error
ColoredPoint() {
super();
}
ColoredPoint(int x, int y) {
super(x, y);
}
}
class Test {
public static void main(String[] args) {
ColoredPoint c = new ColoredPoint(5, 0); // error
//c.reset(); // error
System.out.println(c.x);
}
}
ENUM
import java.util.List;
import java.util.ArrayList;
class Card implements Comparable<Card>, java.io.Serializable {
public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,JACK, QUEEN, KING, ACE }
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
private final Rank rank;
private final Suit suit;
public Rank rank() {
return rank;
}
public Suit suit() {
return suit;
}
private Card(Rank rank, Suit suit) {
if (rank == null || suit == null)
throw new NullPointerException(rank + ", " + suit);
this.rank = rank;
this.suit = suit;
}
public String toString() {
return rank + " of " + suit;
}
// Primary sort on suit, secondary sort on rank
public int compareTo(Card c) {
int suitCompare = suit.compareTo(c.suit);
return (suitCompare != 0 ? suitCompare : rank.compareTo(c.rank));
}
private static final List<Card> prototypeDeck = new ArrayList<Card>(52);
static {
for (Suit suit : Suit.values()) {
System.out.println(suit);
for (Rank rank : Rank.values())
prototypeDeck.add(new Card(rank, suit));
}
}
// Returns a new deck
public static List<Card> newDeck() {
return new ArrayList<Card>(prototypeDeck);
}
public static void main(String[] args) {
System.out.println(prototypeDeck);
}
}
------------- ------------------- ----------------------- ---------------------
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class Deal {
public static void main(String args[]) {
int numHands = Integer.parseInt(args[0]);
int cardsPerHand = Integer.parseInt(args[1]);
List<Card> deck = Card.newDeck();
System.out.println(deck);
Collections.shuffle(deck);
System.out.println(deck);
for (int i=0; i < numHands; i++)
System.out.println(dealHand(deck, cardsPerHand));
}
public static <E extends Comparable<E>>
ArrayList<E> dealHand(List<E> deck, int n) {
int deckSize = deck.size();
List<E> handView = deck.subList(deckSize - n, deckSize);
ArrayList<E> hand = new ArrayList<E>(handView);
handView.clear();
Collections.sort(hand);
return hand;
}
}
Output:
CLUBS
DIAMONDS
HEARTS
SPADES
[DEUCE of CLUBS, THREE of CLUBS, FOUR of CLUBS, FIVE of CLUBS, SIX of CLUBS, SEVEN of CLUBS, EIGHT of CLUBS, NINE of CLUBS, TEN of CLUBS, JACK of CLUBS, QUEEN of CLUBS, KING of CLUBS, ACE of CLUBS, DEUCE of DIAMONDS, THREE of DIAMONDS, FOUR of DIAMONDS, FIVE of DIAMONDS, SIX of DIAMONDS, SEVEN of DIAMONDS, EIGHT of DIAMONDS, NINE of DIAMONDS, TEN of DIAMONDS, JACK of DIAMONDS, QUEEN of DIAMONDS, KING of DIAMONDS, ACE of DIAMONDS, DEUCE of HEARTS, THREE of HEARTS, FOUR of HEARTS, FIVE of HEARTS, SIX of HEARTS, SEVEN of HEARTS, EIGHT of HEARTS, NINE of HEARTS, TEN of HEARTS, JACK of HEARTS, QUEEN of HEARTS, KING of HEARTS, ACE of HEARTS, DEUCE of SPADES, THREE of SPADES, FOUR of SPADES, FIVE of SPADES, SIX of SPADES, SEVEN of SPADES, EIGHT of SPADES, NINE of SPADES, TEN of SPADES, JACK of SPADES, QUEEN of SPADES, KING of SPADES, ACE of SPADES]
[QUEEN of CLUBS, SEVEN of CLUBS, NINE of HEARTS, TEN of DIAMONDS, QUEEN of SPADES, KING of DIAMONDS, FOUR of HEARTS, SIX of DIAMONDS, FOUR of DIAMONDS, SIX of SPADES, JACK of DIAMONDS, TEN of HEARTS, JACK of HEARTS, EIGHT of SPADES, SEVEN of SPADES, KING of SPADES, ACE of DIAMONDS, NINE of CLUBS, FIVE of SPADES, THREE of CLUBS, ACE of SPADES, THREE of DIAMONDS, NINE of SPADES, SIX of HEARTS, DEUCE of HEARTS, DEUCE of DIAMONDS, FIVE of CLUBS, FOUR of SPADES, QUEEN of HEARTS, ACE of HEARTS, FIVE of HEARTS, EIGHT of HEARTS, FIVE of DIAMONDS, ACE of CLUBS, SIX of CLUBS, DEUCE of SPADES, DEUCE of CLUBS, JACK of CLUBS, JACK of SPADES, KING of HEARTS, NINE of DIAMONDS, QUEEN of DIAMONDS, KING of CLUBS, EIGHT of CLUBS, TEN of CLUBS, SEVEN of HEARTS, THREE of HEARTS, EIGHT of DIAMONDS, SEVEN of DIAMONDS, THREE of SPADES, TEN of SPADES, FOUR of CLUBS]
[FOUR of CLUBS, SEVEN of DIAMONDS, EIGHT of DIAMONDS, THREE of SPADES, TEN of SPADES]
[EIGHT of CLUBS, TEN of CLUBS, KING of CLUBS, THREE of HEARTS, SEVEN of HEARTS]
[JACK of CLUBS, NINE of DIAMONDS, QUEEN of DIAMONDS, KING of HEARTS, JACK of SPADES]
***************** ************************ ****************** *************