|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Defining Java ClassesClasses function like blueprints that define the attributes and behaviors that objects have after theyve been created or instantiated. When a program creates an object, it creates an instance of an objects 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 objects class. Declaring ClassesThe 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 }
Normally, the classes you create wont have modifiers, and in many cases they wont 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 VariablesInstance 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.
Using Class VariablesClass 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 MethodsAll class methods, except for constructors, must have a return type. This return type can be of any fundamental data type or object. Methods that dont 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.
Ive 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 thats a combination of the two strings that were passed in. The class Ive 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 MethodsIn 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. Ive 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 youre 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 ); } }
|
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. |