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

 javacovericon

A Simple Example

To see how it all works, we need an example that not only demonstrates how the Java language works to let you create classes and objects, but something that demonstrates why you might go to the trouble of using it.

Consider for the moment that you need to perform some complicated calculations. You could just write a set of functions, one for each calculation, but in many cases it would be better to group them together so that they could be used as properties and methods of an object.

For example, suppose you needed to work with circles drawn on a graphic display. Then it makes sense to create a class that encapsulate all of the things you might want to do with a circle.

Start a new project called MathExample.

Add a new class called Circle.

The basic properties of a circle are its position x,y and its radius r - so let's add these as properties to the Circle class:

 public class Circle{
    public double x;
    public double y;
    public double r;

 

They are public and so accessible from outside of the class.

The data type double is a double precision number.

We also need to use the value of the constant Pi and there is no reason why we should make this available to the outside world so it can be private:

private double Pi=3.14159;

As well as storing the size and position of the circle it would be useful to have the ability to discover the circumference and so we add a public circumference method:

public double circumference(){
        return 2*Pi*r;
    }

In the same way it would be nice to discover the area of the circle - so lets add an area method. But to do this we need to compute r squared. Let's add a private squared function that simply returns the square of any number:

private double square(double a) {
        return a * a;
    }

 

You could argue that square was sufficiently useful to make public but in the spirit of hiding how the calculations are performed we can also chose to make it private.

Finally the area function is:

public double area() {
        return Pi * square(r);
    }

 

The complete list for the class is:

public class Circle {
    public double x;
    public double y;
    public double r;
    private double Pi = 3.14159;

    public double circumference() {
        return 2 * Pi * r;
    }
    private double square(double a) {
        return a * a;
    }

    public double area() {
        return Pi * square(r);
    }
}

 

Now let's write a main function that makes use of the class:

 

public static void main(String[] args) {
    Circle circle = new Circle();
    circle.r = 10.5;
    circle.x = 0;
    circle.y = 0;
    double circ = circle.circumference();
    double area = circle.area();
    System.out.println(circ);
    System.out.println(area);
 }

This starts off by creating a circle object from the class.

Notice that we have the problem of finding a suitable name for the class and the object - they can't both be called Circle.

This is a standard problem of class based object oriented languages and there are many ways around it. One is to always start a class name with an uppercase letter and use lowercase for objects.

So in our example the class is Circle and the object is circle - remember Java is case sensitive. In practice this name problem isn't as big in real life programming  as it is in examples because you generally have more specific names ready to use for instances of a class - bigcircle, bluecircle, offsetcircle and so on.

After creating the circle object we set its radius and position and then use its methods to get the circumference and area which are displayed in the output window.

Notice you can create any number of circles now you have the Circle class and each one is independent of the others and will compute its own area and circumference.

This is just one of the reasons object-oriented programming is a good idea - there are many more more sophisticated reasons for using it.

Before ending this chapter, I should add that our example doesn't do things in the best possible way.

There are better ways to do things. For example, instead of using public variables as properties it is better to use methods to acces private variables. That is instead of letting users of the class directly access x,y an r we would introduce accessor functions like getX and setX. This is so standard in Java that giving direct access to member variables is almost never encountered. However when you are learning Java you need to know how things actually work. We will return to get and set methods later. 

There's still a lot to learn  about classes and objects, including constructors, inheritance and more.

These ideas are the subject of later chapters.

 

 

Modern Java
With NetBeans And Swing

largecover

Contents

  1. Why Java?
  2. Getting started with Java
  3. Introducing Java - Swing Objects
  4. Writing Code
  5. Command Line Programs
  6. User Interface - More Swing
  7. Working With Class
  8. Java Class Inheritance
  9. Java Data Types - Numeric Data
  10. Java Data Types - Arrays And Strings
  11. Building a Java GUI - Containers
  12. Advanced OOP - Type, Casting, Packages
  13. Value And Reference 
  14. Java Lambdas, SAMs And Events

 

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin,  or sign up for our weekly newsletter.

 

graphics

 



 

Comments




or email your comment to: comments@i-programmer.info