|
Java Tutorial |
ClassesInDetailThese 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 Pretty much every method we have created or used so far has been a Try this. Create a new class called
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 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 For examples of using imports see the next section. This work is Copyright Chris Hunter 2007, you may use it for non-commercial purposes |