Java: Class Structure

There may be one or two people who click onto this article expecting a discussion on socialism, sorry to disappoint, but it’s still about Java!

We touched briefly on class structure in the last post, but now I want to dive into a bit more detail on the different components of a java class. Most of it is pretty basic, but there may be one or two parts that surprise you.

Basic Class Structure

Java classes need to follow the structure of Package – Import – Class (PIC is a handy way to remember that).

package mypackage.myotherpackage;

import java.util.*;

public class MyClass {

The code snippet above shows the basic structure, it’s important to remember that both the package and imports are optional, but the order must be kept. If you don’t declare a package, the class is assumed to be in the default package, though this isn’t recommended to do, and will make the people you code with hate you (not that I’m speaking from personal experience).


Lets talk a little bit more about the code in the class brackets. You’re probably used to seeing functions, a constructor, and maybe some variable declarations in there, however we can also place initializers in there. Initializers are pieces of code outside functions wrapped in {}. There are two types, static initializers, and instance initializers, and you can probably take a good guess at the difference between the two of them. Lets look at the code snippet below

package mypackage.myotherpackage;

public class MyClass {

        System.out.println("This is an instance initializer.");

        System.out.println("You can have more than one.");

        System.out.println("This is a static initializer.");

    public MyClass(){
        System.out.println("This is the constructor.");

    public static void main(String[] args) {
        MyClass instance = new MyClass();

This code has both static and instance initializers. Static initializers are run once when the class is loaded and instance initializers are run, you guessed it, when you make a new instance of the class. The code in initializers is run before constructors are run, static initializers are run before instance initializers, and they are run in the order they appear in the file. Using the example above, the output would be the following:

This is a static initializer.
This is an instance initializer.
You can have more than one.
This is the constructor.

Now for a more complex example, what’s the output of the code below?

public class Chick {

    public Chick(){

    public static void main(String[] args){
        {System.out.println("Before instantiate chick.");}	//Code in blocks like this in a function is valid.
        Chick ch = new Chick();
        System.out.println("After instantiating chick.");

    {System.out.println("Instance initializer.");}
    private String name = "Chicky.";
    {name = "Chickenator.";}
    static{System.out.println("Static initializer called.");}

I admit this is pretty awkward, and the line spacing is purposefully left tight to make it a little bit harder to follow (you can thank the Java Certification book for that helpful tip). The output will be as follows, did you get it right?

Static initializer called.
Before instantiate chick.
Instance initializer.
After instantiating chick.

You can see that the static initializer runs before the main method is even called, and then the order of the instance initializers takes precedence.

Inner Classes

Lastly, lets look at some slightly different classes. Java supports putting multiple classes in one file, though at most one class can be public, as in the example below.

public class Meerkat {

    public Meerkat(){
        tail myTail = new tail();
        System.out.println("Tail length is " + myTail.getLength());

    class tail{
        int length;
        {length = 5;} //Instance initiializer
        public int getLength(){
            return length;


//No access modifier allowed here
class paw{
    public static void lickPaw(){

It’s also possible to declare a class within another class, as in the code snippet above, though, like other weird yet valid Java things, it’s usually frowned upon if you do it.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s