Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How Object Initialization Works [Videos]

DZone's Guide to

How Object Initialization Works [Videos]

Object initialization might sound simple, but do you really know it? Can you have a static initializer block in an interface? And where do concrete classes fit in?

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

You may have gotten used to my habit of writing about some special, non-trivial Java feature or usage. This time it will be a little different, at least to start. This is a video tutorial about object initialization. We have an interface. Then we have an abstract class that implements the interface and a concrete class that extends the abstract class. They do nothing except write some strings to the console when they are executed so we can see what order they are executed. They have static initializer blocks, wherever it's possible, non-static initializer blocks, and constructors.

By the way, do you know, and be honest with yourself, whether it's permitted to have a static initializer block inside an interface? If you know Java very well but you cannot answer that question with absolute certainty, it will not hurt to watch this 9-minute tutorial.

And also stay tuned for the second part — when we get back to my real style and let all hell loose.


While you wait for the second tutorial, here is a puzzle:

How is it possible to have an instance of the Concrete class even though calling a new Concrete() throws:
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.javax0.classinit.Concrete

If you could not find the answer to the puzzle, watch the second part of the tutorial:


The source code is available from https://github.com/javax0-tutorials/object-initialization.

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java ,object initialization ,concrete classes ,tutorial ,initializer

Published at DZone with permission of Peter Verhas, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}