home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Defining Java Classes

Classes function like blueprints that define the attributes and behaviors that objects have after they’ve been created or instantiated. When a program creates an object, it creates an instance of an object’s class. Therefore, the word instantiate is used interchangeably with the word create in regard to objects.

The term instantiate means to make an instance of an object’s class.

Declaring Classes

The declaration of a class in Java has a specific syntax. The following code describes the full syntax for the declaration of a Java class:

[modifiers] class Identifier [extends SuperClass]
     [implements Interface {, Interface}]
{
     class body
}


Note:  In the preceding code you might notice two unfamiliar terms: extends and implements. These terms are discussed later in this chapter. Note, too, that the items in brackets are optional.

Normally, the classes you create won’t have modifiers, and in many cases they won’t extend or implement anything. The following example shows you the simplest kind of class declaration, in which we name a class SomeItem, and then put whatever sort of code and data in it that we need:

class SomeItem
{
    body of class
}

Declaring Instance Variables

Instance variables apply to the individual objects that are instantiated from a class. Instance variables are declared within a class definition but outside the body of any methods. An instance variable can be of any type and can be initialized in its declaration. The following code declares the m strType and m_nLength instance variables:

class SomeItem
{
    String m_strType = <"Lamp Shade";
    int m_nLength = 100;
}

If not explicitly initialized, instance variables are assigned default initial values: 0 for number types, false for booleans, and null for objects. To make an instance variable constant, you start its declaration with the keyword final. Any final variables must be initialized. The following code declares the m_strType and the m_nLength variables as final:

class SomeItem
{
    final String m_strType = "Lamp Shade";
    final int m_nLength = 100;
}

Notice that if either one of these final variables were not initialized, the compiler would generate an error.


Tip:  Many Visual J++ programmers are also Visual C++ programmers. If you’re going to be programming in C++ as well as J++, remember to initialize variables you declare in both languages. This is good programming practice, and it helps to ensure that you don’t forget to initialize your C++ variables. Because C++ does not initialize variables for you, the habit of explicitly initializing variables can save you a lot of debugging time.

Using Class Variables

Class variables are similar to instance variables except that they apply to all the objects instantiated from a class. Only one value per each class value can exist, regardless of how many objects have been created. Class variables exist even if no instances of a class exist. They are the equivalent of static members in C++. Not surprisingly, class variables are declared using the static modifier. The following example shows how to declare a static string. This string will be the same for all instantiations of this class.

class SomeItem
{
    static String m_strType = "Lamp Shade";
    final int m_nLength = 100;
}

A static variable can be accessed via its class name, as shown in the following line of code:

SomeItem.m_strType = "Other Lamp Shade";

Declaring Methods

All class methods, except for constructors, must have a return type. This return type can be of any fundamental data type or object. Methods that don’t return anything must be declared with a return type of void. The following code describes the syntax for the declaration of a Java method:

ReturnType MethodName( type arg1, type arg2. … )
{
    body of the method
}

Java class methods can have an optional parameter list. The parameter list records type and name pairs separated by commas. All parameter passing in Java is done by value.


Note:  When objects are passed to a method, a value representing the object’s reference is passed. This process makes it possible for the target method to modify the contents of the object. It is very important to remember, however, that you can’t change the value of a fundamental data type, such as int or float, in the target method.

I’ve created a class that has two methods. The first method takes one argument that is an integer. It returns an integer. The second method takes two strings as arguments. It puts them together and returns a string that’s a combination of the two strings that were passed in. The class I’ve created with these two methods is shown here:

class SomeItem
{

    int m_nMultiplier = 15;
    // This method takes one integer argument and returns an integer.
    public int getMultiple( int nNumber )
    {

        return( m_nMultiplier * nNumber );
    }

    // This method takes two Strings as arguments and returns a String.
    public String concatenate( String strOne, String strTwo )
    {
        String strEntire;

        strEntire = strOne + strTwo;

        return( strEntire );
    }

    }

Overloading Methods

In Java, overloaded methods can be defined with the same name, but the methods must have different parameter lists. This is known as method overloading. Methods can be overloaded by the type or number of their parameters. Overloading a return type is not supported. When a method is invoked, Java tries to match the method name and parameter list used in the call with the predefined version of the method. I’ve created a new class called SomeOtherItem. It has two methods, both of which are named concatenates. The first of these takes two strings as arguments. It combines them and returns the entire string as the return type. The second concatenate() method takes two arguments; but whereas the first one is a still a string, the second one is an integer. The method then takes both of these and combines them into a single string and returns that string. When you’re using this class and you call the concatenate() method and pass it two strings, it already knows which of these two methods to use. If you call a concatenate() method and you have the first argument as a string and the second argument as an integer, again it knows which of these two methods to call and will use the appropriate method.

The following class shows two concatenate() methods that are overloaded:

class SomeOtherItem
{
    // This method takes two Strings as arguments and returns a String.
    public String concatenate( String strOne, String strTwo )
    {
        String strEntire;

        strEntire = strOne + strTwo;

        return( strEntire );

    }

    public String concatenate( String strOne, int nTwo )
    {

        String strEntire;

        strEntire = strOne + nTwo;

        return( strEntire );

    }

}


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.