Java Working With Class
Banner
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.

 

Modern Java
With NetBeans And Swing

covericon

Contents

  1. Getting started with Java

    In chapter 1 we tell you how to get started with modern Java development in the shortest possible time. The approach uses NetBeans and Swing and all of the resources used are free to download and use.

  2. Introducing Java - Swing Objects
    In the second chapter of our beginner's guide to Modern Java we find out more about objects by exploring the Swing framework with a simple hands-on example.

  3. Writing Code

    Using ifs and loops is one of the most difficult parts of learning how to program. Our beginners introduction to Java reaches the part all programmers have know and know well - how to write code.

  4. Command Line Programs
    Command line programming means doing things in the simplest possible way. We take a careful look at how data types and code build a program.

  5. User Interface - More Swing
    Finding out how to create a User Interface (UI) using the Java Swing library is not only a useful skill, it also is an ideal way to learn about objects and to make sure that the ideas really have sunk in.

  6. Working With Class
    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.

  7. Java Class Inheritance
    Working with classes and objects is a very sophisticated approach to programming. You can't expect to absorb all of its implications in one go. We have already looked at the basics of class and objects. Now we need to look at encapsulation, constructors, overloading and inheritance.

  8. Building a Java GUI - Containers
    In this chapter we get to grips with the idea of a container that is used to host components to build a user interface. We also find out how the Swing GUI Builder generates code to make it all much easier. 

  9. Java Data Types - Numeric Data
    After looking at some of the advanced ideas of classes and objects we need to return to some simpler topics to make our understanding complete. We need to look more closely at data and, to get things moving, numeric data. 

 

 

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 ( Thursday, 09 June 2016 )
 
 

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