Recent Changes - Search:

Java Tutorial

edit SideBar

ClassesInDetail

These pages make up the course notes for my Java programming course (run at Dallam, Milnthorpe, Cumbria). Hopefully they also make a useful self-learning tutorial.

Classes, Objects, Packages and Imports

Up to now we have created a few classes, but haven’t really talked about what a class is or why it is called a class. We have also skirted around the issue of things like String and Float – which are themselves classes – that we used, but without creating our own String or Float class.

Think of Classes as “blueprints” (or plans, or designs) for things. Given a blueprint we can manufacture any number of the thing shown on the blueprint. The things that we “manufacture” from Classes are known as Objects. In this way the String class is a blueprint for a String of characters and we can have any number of String Objects created from the String class.

As mentioned earlier the Java language is made up of just under 50 “words”, on their own they leave you, the programmer, with an awful lot of work to do because they only provide the most basic elements. Java comes with a large “library” of standard classes (see http://java.sun.com/j2se/1.5.0/docs/api/ but don’t be put off by it). In that list you will find the String class and the Float class, and you will find brief descriptions of the methods they provide.

Try to find the Float class in the list on the lower left (in http://java.sun.com/j2se/1.5.0/docs/api/ ) – look at the description for the parseFloat method. We used the parseFloat method in the VatBill example to convert a String to a floating point number.

Pretty much every method we have created or used so far has been a static method. This makes a very important difference. static methods are methods that can be invoked (used, called) on the class directly without creating an Object from the class first. The vast majority of methods that you create will not be static.

Try this. Create a new class called StaticDemo (Follow the instructions in the "your First Program" section to create a class file) and paste the code below into it.

public class StaticDemo {


        public static void staticMethod() {
                System.out.println("Hello from a static method");
        }

        public void normalMethod() {
                System.out.println("Hello from a normal, non-static method");
        }

        public static void main(String[] args) {

                StaticDemo.staticMethod();

                StaticDemo.normalMethod();

        }

}

What you will find is an error on the line StaticDemo.normalMethod(); . This is because normalMethod is not static (its method declaration does not include the static keyword), and so we must create an object from the StaticDemo class before we can use it. To create an object we use the new key word. Change the line
StaticDemo.normalMethod();

to

StaticDemo demo = new StaticDemo();
demo.normalMethod();

The error should go away and you should be able to run the class.

Notice that the main method is always static - think about why this would be Hint

As I mentioned before, most methods you create will be non-static (like the normalMethod in the example above). But you might be asking Why Bother??? . Surely it is easier to make everything static and just get on with it.

There are a number of reasons why not, but primarily we want to ensure that each instance of an object is separate from any others. Consider Strings. If I create a String object holding the character String "Fred" and then create another one holding "Bill" I want two separate String objects. I don't want the String "Bill" to overwrite "Fred". static methods operate at class level and therefore may have an effect on any object created from the class (it's a bit like using a biscuit cutter to make biscuits, changing a biscuit after it has been cut does not affect other biscuits, changing the cutter affects all biscuits made with it).

If you are not convinced then you are just going to have to accept that the way it is done is to use non-static methods. Static methods and variables are only used When there is a good reason to do so.

So to recap a little, a class is a class (category, type, kind) of thing and an object is a particular "instance" (item, example) of that thing. For example we use Files to store things, so File is a class of thing, a file called "JobApplication.doc" is a specific instance of a file and would be a File Object. As another example consider telephones. A telephone is a class of thing (we might say that a telephone is a class of communications device). My telephone, which might have a number of 01234 56789 is a telephone object. All telephones have a number, states (on hook, off hook) and methods (dial, redial, etc) - these aspects are key to any object, which should have identity, state and behaviour.

Packages and Imports

Earlier in this section we looked briefly at all the classes that come as standard with Java. Before we can start using these we need to understand two more things, these are Packages and imports.

Until now we have just created classes, and you may have noticed a warning in EasyEclipse - "Use of the default package is discouraged" - that we have been ignoring. Similar to the way that we can have several methods collected together in a class, a package is used to group together several classes.

A package, as far as classes that we create are concerned, equates to a folder (or directory) on the local computer. So if you create a class and supply a packag name, the class file will be created in a folder of that name.

So when you create a new class, you really should put it in a package Note

The other thing you need to know about packages is that classes in a package (i.e. classes in the same package as each other) can see each other, but other classes cannot. So classes in one package cannot see classes in another directly. To overcome this restriction we need to import the classes from other packages. You are going to be using import quite a lot.

Note that we did not have to import the String, Float and System classes. Some classes are so commonly used that they are automatically imported. They are in a package called java.lang Have a look back at the standard java classes ( at http://java.sun.com/j2se/1.5.0/docs/api/ - get used to looking at that - book mark it and learn how to open it in another tab or browser window) and find the java.lang package in the top left window, you will see a list of the classes it contains, and which are available by default Don't be put off, you don't need to know what they all do, when you need something, you can go and have a look

For examples of using imports see the next section.

 

Creative Commons License

This work is Copyright Chris Hunter 2007, you may use it for non-commercial purposes
under the Creative Commons license Creative Commons Attribution-Noncommercial-Share Alike.

Edit - History - Print - Recent Changes - Search
Page last modified on July 03, 2007, at 02:04 PM