Static field, constructor and exception : Static « Class « Java






Static field, constructor and exception

  

public class Main {
  static Bar bar;

  static {
    try {
      bar = new Bar();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] argv) {
    System.out.println(bar);
  }
}

class Bar {
  public Bar() throws Exception {
  }

  public String toString() {
    return "Bar";
  }
}

   
    
  








Related examples in the same category

1.Java static member variable example
2.Java static method
3.Using Static VariablesUsing Static Variables
4.Static Init Demo
5.Show that you do inherit static fieldsShow that you do inherit static fields
6.Show that you can't have static variables in a method
7.Explicit static initialization with the static clause
8.This program demonstrates static methods