Thinking in Java Fourth Edition Bruce Eckel Chapter 4 Exercise 15

Exercise 15: Create a class with a String that is initialized using instance initialization.

class Tester
{
    String s;
    {
        s = "Initializing string in Tester";
        System.out.println(s);
    }
    Tester()
    {
        System.out.println("Tester()");
    }
}

public class InstanceClauseTest
{
    public static void main(String[] args)
    {
        new Tester();              
    }
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *