Java
Java intro
Java basics
Java variables
Java conditionals
Java loops
Java arrays
Java OOP 1
Java OOP 2
Java interfaces
Java Strings
Java user input
Java exceptions
Java packages
Java rand numbers
Java GUI's
Java GUI layout
Java events
Java applets
Java graphics
Java sounds
Java summary

Programming

Programming intro

Markup

First webpage guide HTML
XHTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

Java classes and objects (part 1)

Java is an object-oriented language. Other object-oriented languages include PHP, Javascript, and C++. An object-oriented language is a language which supports the creation of data types which go beyond the simple primitive data types. These particular data types are called objects. These particular data types have much more power and functionality than primitive data types. An object in the context of object-oriented programming is a representation of a real world thing.

This lesson focuses on:

Creating a class

A class is a blueprint for an object. As stated above in the introduction to this lesson, an object is a representation of a real world thing. An object can represent a car, a table, a book or any other real world concept. Before an object can be implemented, however, a blueprint has to be designed for it, and that blueprint is the class of an object. A class is a blueprint for an object stating the various properties (variables) of the object as well as what it can do (methods).

Syntax for creating a class:

class className{

}

A class is created with the class keyword, followed by a single space, the name of the class to be created, an opening curly brace, and a closing curly brace. All the specifics and logistics of the class go in between these curly braces.

Example of a class:

class Book{

}

The above code creates a class named Book.

Classes can be declared with certain keywords called access modifiers which dictate the level of access on a class.

Class access modifiers:

Creating variables for a class

Class variables are known as member variables, because they store important information in regards to the class.

Example:

class Book{

    String title;
    int numPages;

}

The above code uses the Book class and creates two member variables for it. The first is title - a string variable which will store the title of the book. The second is numPages - a numeric variable which will store the number of pages in the book.

Variables can be declared with certain keywords called access modifiers which dictate the level of access on a variable.

Variable access modifiers:

Creating methods for a class

Class methods are defined inside a class definition, they can be used to either retrieve data or manipulate data. Class methods which are used to retrieve data are known as accessor methods. Class methods which are used to manipulate data are known as mutator methods.

Example:

class Book{

    String title;
    int numPages;

    //this is a mutator method
    public void setNumPages(int numOfPages){

        numPages = numOfPages;

    }

    //this is an accessor method
    public int getNumPages(){

        return numPages;

    }

    //this is a mutator method
    public void setTitle(String theTitle){

        title = theTitle;

    }

    //this is an accessor method
    public String getTitle(){

        return title;

    }

}

In the above example, four methods are created:

Methods can be declared with certain keywords called access modifiers which dictate the level of access on a method.

Method access modifiers:

Another keyword used with methods is the void keyword. The void keyword is used to specify that a method will not return a value. To specify that a method does return a value, instead of the void keyword, the appropriate keyword corresponding to the data type that the method will return should be used. For example, a method that will return an integer should have the keyword int in front of it. Generally, mutator methods do not return a value, and accessor methods do return a value.

The example from above contains two methods that do not return a value and two methods that do return a value.

The example from above reiterated:

class Book{

    String title;
    int numPages;

    //does not return a value
    //use the keyword void
    public void setNumPages(int numOfPages){

        numPages = numOfPages;

    }

    //returns an int value
    //use the keyword int
    public int getNumPages(){

        return numPages;

    }

    //does not return a value
    //use the keyword void
    public void setTitle(String theTitle){

        title = theTitle;

    }

    //returns a String value
    //use the name of the class (String)
    public String getTitle(){

        return title;

    }

}

Instantiating a class

So you have created your class with its variables and methods, and now what to do with it? Now you can create objects based on how you designed your class. The process of creating an object is called instantiation.

Syntax:

nameOfClass nameOfObject = new nameOfClass();

Example:

Book YellowPages = new Book();

The above example creates an object named YellowPages which is an instance of the Book class.

Creating a class constructor

What if you want an object to automatically have a certain value when it is instantiated? For example, if you wanted all instances of the Book class to automatically have 200 pages? This is what class costructors are for. Class constructors are used to set objects to have certain values when they are initialized. The method used for class constructors takes the same name as the class for which it will be a constructor.

NOTE: A class constructor should be declared within the class that it is a constructor for.

Syntax for a class constructor:

name_Of_Class_To_Create_A_Constructor_For([parameters]){

}

Example:

class Book{

    String title;
    int numPages;

    public Book(int numPages){

    this.numPages = numPages;

    }

}

In the above example, the Book class contains a constructor with one parameter. The value given to this parameter will be how many pages the book has.

Did you notice a new keyword in the above example? The keyword this is a special keyword used when creating classes. The purpose of it is to refer to the current class.

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments