class User { private String name; private static int count; static { User.count = 0; System.out.println("Static initializer"); } public User(String name){ this.name = name; User.count++; System.out.println("Constructor initializer"); } { System.out.println("Instance initializer"); } public static void getInfo(){ System.out.println("# of instances: " + User.count); } } public class MyApp { public static void main(String[] args){ User.getInfo(); User tom = new User("Tom"); User.getInfo(); User john = new User("John"); User.getInfo(); } }
[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Static initializer
# of instances: 0
Instance initializer
Constructor initializer
# of instances: 1
Instance initializer
Constructor initializer
# of instances: 2
final は継承できない。
final class User { protected String name; private static final double VERSION = 1.1; static { this.name = name; User.VERSION = 1.2; } public final void sayHi{ System.out.println("Hi! " + this.name); } public static void getInfo(){ System.out.println("# of instances: " + User.count); } }
抽象クラス
// 抽象クラス -> 具象クラス abstract class User { public abstract void sayHi(); // 抽象メソッド } class JapaneseUser extends User { @Override public void sayHi(){ System.out.println("こんにちは!"); } } class AmericanUser extends User { @Override public void sayHi(){ System.out.println("Hi"); } } public class MyApp { public static void main(String[] args){ AmericanUser tom = new AmericanUser(); JapaneseUser aki = new JapaneseUser(); tom.sayHi(); aki.sayHi(); } }
[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Hi
こんにちは!
interface
interface Printable{ // 定数 double VERSION = 1.2; // 抽象メソッド void print(); // default メソッド public default void getInfo(){ System.out.println("I/F ver. " + Printable.VERSION); } // static メソッド } class User implements Printable { @Override public void print(){ System.out.println("Now printing user profile..."); } } public class MyApp { public static void main(String[] args){ User tom = new User(); tom.print(); tom.getInfo(); } }
[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Now printing user profile…
I/F ver. 1.2
列挙型
enum Result { SUCCESS, ERROR, } public class MyApp { public static void main(String[] args){ Result res; res = Result.ERROR; switch (res){ case SUCCESS: System.out.println("OK!"); System.out.println(res.ordinal()); break; case ERROR: System.out.println("NG!"); System.out.println(res.ordinal()); break; } } }
[vagrant@localhost java]$ java MyApp
NG!
1
例外処理
public class MyApp { public static void div(int a, int b){ System.out.println(a / b); } public static void main(String[] args){ div(3, 0); } }
[vagrant@localhost java]$ java MyApp
Exception in thread “main” java.lang.ArithmeticException: / by zero
at MyApp.div(MyApp.java:13)
at MyApp.main(MyApp.java:17)
例外処理
class MyException extends Exception { public MyException(String s){ super(s); } } public class MyApp { public static void div(int a, int b){ try { if (b < 0){ throw new MyException("not minux!"); } System.out.println(a / b); } catch (ArithmeticException e){ System.err.println(e.getMessage()); } catch(MyException e){ System.err.println(e.getMessage()); } } public static void main(String[] args){ div(3, 0); div(5, -2); } } [/java] Wrapper Class [java] public class MyApp { public static void main(String[] args){ // Integer i = new Integer(32); // int n = i.intValue(); Integer i = 32; // auto boxing i = null; int n = i; // auto unboxing System.out.println(); } } [/java] [vagrant@localhost java]$ javac MyApp.java [vagrant@localhost java]$ java MyApp Exception in thread "main" java.lang.NullPointerException at MyApp.main(MyApp.java:14) 型を後程指定するジェネリック [java] class MyData<T> { public void getThree(T x){ System.out.println(x); System.out.println(x); System.out.println(x); } } public class MyApp { public static void main(String[] args){ MyData<Integer> i = new MyData<>(); i.getThree(32); } }
[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
32
32
32