Java: Imports & Constructors

Well, let’s jump right into it, shall we? I’ll cover the basics of imports and constructors here, plus many of the fun ways you can break them by writing confusing code.

Imports

Imports are how you indicate which packages you wish to use classes from. The basic syntax is below:

import java.util.Date; //import the Date class
import java.util.*; //import everything in the util package,
                    //but nothing in sub packages

Pretty straightforward, right? Importing packages in this way lets you use them without having to refer to them by the package name and class each time you want to use it. Otherwise all your code would look something like this:

public class myClass{

    public static void main(String[] args) {
        java.util.Date myDate = new Date();
    }
}

Adding in an import statement gives us this:

import java.util.Date;

public class myClass{

    public static void main(String[] args) {
        Date myDate = new Date();
    }
}

Much neater, right? Now let’s break it!

import java.util.Date.*; //Trying to import everything

In the snippet above we’re trying to import everything that’s… in a class, which doesn’t make sense, so the compiler will complain. What are some other ways to cause issues? If we have a class with the same name in two different packages, and try importing them both, the compiler will complain (approximately 85% of a compilers job is complaining).

import java.util.Date;
import java.sql.Date;

This causes an issue because if you reference a Date in your code, it’s not clear if you want to use the util Date class, or the sql Date class, which makes sense if you think about it. What about this?

import java.util.Date;
import java.sql.*;

This is fine, because util.Date takes precedence. If you refer to Date in your code, the compiler assumes you meant the one from java.util.Date.

Question time! What will happen if you do this?

import java.util.*;
import java.sql.*;

public class myClass{

    public static void main(String[] args) {
        Date myDate = new Date();
    }
}

This will cause a problem, as I’m sure you’ve noticed, because again, the compiler doesn’t know what date you’re referring to. What if you want to import all from both those packages though, how do you get around this? Simple, just explicitly set the package you want to use in your code

java.sql.Date myDate = new Date();

Sorted!

Constructors

I’m just going to say a little bit about constructors here, and will most likely dive into them a little deeper in another post. Constructors follow the basic syntax of

public ClassName(){}

You need to make sure that ClassName is the exact name of your class, keeping an eye on capital letters, as Java is case-sensitive. You’ve probably written code before that ran fine without that constructor though, right? That’s because the compiler will automatically create a default constructor for you, if you don’t provide one in the code. There’s some more detail to go into on that, but again, I’ll reserve it for later.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s