Java Working With Class
Written by Ian Elliot   
Monday, 30 July 2012
Article Index
Java Working With Class
Hands-on example
Why class and objects?
MathExample

The Swing components have provided an easy approach to the idea of objects, but there comes a time when you have to find out how to create your own. In this part of Modern Java, we look at the standard ideas of object-oriented programming.

 

cover

Modern Java Contents

  1. Getting started with Java
  2. Introducing Java - Swing Objects
  3. Writing Java Code - Methods
  4. Command Line Programs
  5. The Java User Interface - More Swing
  6. Working With Class
  7. Class Inheritance
  8. Building a Java GUI - Containers

 

We have encountered objects in Java in earlier chapters. When you drag a button onto a form you are working with a Java object. However, a lot of the work is done for you by the NetBeans IDE. When it comes to creating and working with your own objects you can't expect the same sort of support.

Class, object and instance

In particular, we have to make the distinction between a class and an object. A class is a like a recipe or a blue print for an object. Consider the button that sits in the Netbean's toolbox. This isn't actually a button but a template for a button that you can use to stamp out real buttons. You can think of the button in the toolbar as being a button class and the real buttons that it can create are objects or instances of the class.

We need to learn some jargon.

A class is a specification for an object but it isn't an object.

Using a class to create an object is described as creating and instance of the class.

The act of creating an object from a class is called instantiation.

You also need to know that while buttons and similar are good examples of class and object not all classes and objects have a visual representation. Not all objects are user interface components. So while it helps to think about user interface components when you are learning about class and object things aren't 100% the same.

One big difference is that to create our own objects we first have to create our own class. So let's see how this works. 

What's in a Class?

A class definition looks a lot like the sort of Java code we have been writing up to this point. This is a good thing because it looks familiar but it is also a bad thing because you need to keep in mind that you can't acutely run the code in a class unless you first create an instance of it.

That is a class definition is a template for a chunk of program. It can include variables and functions but when you write it the program doesn’t actually exist anywhere all you have is a recipe for making the program when you want to.

For example, if you write a simple class called Point which is going to be used in our programs to represent a 2D point with an x and y co-ordinate. That is a point object is going to store two numbers representing x and y as properties of the object, its class definition would be -

public class Point {
    public int x;
    public int y;   
}

 

Don’t worry for the moment about the use of public, all this means is that class and variables are accessible from outside of the class. 

Concentrate on the fact that what you have defined here is a template that can be used to “stamp out” multiple copies of this code object.

At the moment there is no variable called x and no variable called y allocated in memory waiting for you to use them. To make something happen you have to take the class definition and use it to create an example, or an "instance", of the class or an object. 

Unfortunately in Java creating an object is a bit more complicated than creating a simple variable but the process is more or less the same once you have untangled it. Firstly you need a variable of the right type to store the new object. This you create just as you would any other variable. For example,

Point Current;

declares a variable called Current suitable for storing a point object i.e an instance of the Point class. 

Compare this with

int x;

which declares a variable suitable for storing an integer.

After the variable has been declared it doesn’t actually contain an example of the class - just in the same way x doesn't contain an particular integer. It’s just ready and waiting to store a new class and the keyword “new”  is exactly how you go about making an instance of the class.

For example

Current=new Point();

creates an instance of the class and stores it in Current

Properties

We have already met properties as part of our look at Swing components but there is a more basic form of property that we need to master first.

We have just created an instance of the Point class. Now there are two integer variables, called x and y, waiting for you to store data in them but you don’t refer to them just as x and y. To gain access to any of the components or “members” of an instance of a class you have to specify the particular instance name as well as the member name.

Put more directly to store something in x you would write

Current.x=10;

In general when you create an instance called myobject you refer to any of its members using a fully qualified name:

myobject.myproperty

If you are wondering why it isn't

Point.x=10;

then you need to remember that it is the instances you create from the Point class that are objects you can work with. Point is simply a template that you can use to create Point objects.



Last Updated ( Wednesday, 07 November 2012 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.