Beruflich Dokumente
Kultur Dokumente
3.2 Constructors
Instantiation is made possible by the use of a constructor, which serves several
purposes.
• A constructor is given the same name as the class to allow for the data
type of objects to be declared.
• A constructor is normally used in conjunction with the keyword new,
which allocates memory space from the heap. The heap is an area of
memory set aside for the dynamic allocation of computer memory to
objects during run time.
1
• A constructor provides the storage in memory and the initialization of the
instance variables allocated to the object.
• For each separate invocation of the constructor, a new object will
become instantiated.
SYNTAX
Constructor:
public class-name ( formal-parameter-list )
{
declarations
statements
}
SYNTAX
Instantiation:
object-name = new class-constructor();
object-name = new class-constructor(argument-list);
where argument-list consists of one or more values used by the constructor to
initialize the data of the object.
2
Figure 2.1 Packages and classes
Figure 2.2 illustrates how three primitive types int, char, and float can be
conceptually represented in the memory of the computer.
In Figure 2.2, the values of the identifiers year, letter, and tax are stored at the
memory locations depicted by the names of the identifiers. Hence, the
primitive data is stored by value.
Figure 2.3 illustrates that when an identifier of the type String is initialized, the
value of the string is not stored at the memory location depicted by the
identifier, but it is stored in a different location pointed at or referenced by the
identifier. The object alphabet is stored by reference.
3
Figure 2.3 An object is stored by reference
From the partial listing of the String class, examples of command messages are
concat, replace, and toUpperCase; an example of a query message is length.
These messages are implemented as instance methods.
SYNTAX
Passing a Message to an Object by an Instance Method:
object.method-name();
object.method-name(argument-list);
// program to demonstrate the String class and some of its instance methods
import avi.*;
class Example_1
{
public static void main(String[] args)
{
String oldString = "Have a nice day!";
String newString = oldString.replace('a','-');
String capitalString = oldString.toUpperCase();
int lengthOfString = oldString.length();
Window screen = new Window("Example_1.java","bold","blue",36);
screen.showWindow();
screen.write("\n\tOld string: " + oldString + "\n");
screen.write("\n\tNew string: " + newString + "\n");
screen.write("\n\tOld string in upper case: " + capitalString +"\n");
screen.write("\n\tLength of old string: " + lengthOfString +"
characters\n");
}
}
4
package stringku;
/**
* @author Eto
*/
public class Main {
public static void main(String[] args) {
String oldString = "Have a nice day!";
String newString = oldString.replace('a','-');
String capitalString = oldString.toUpperCase();
int lengthOfString = oldString.length();
String gabung= oldString.concat(" and always Fun");
System.out.println("oldString : "+oldString);
System.out.println("newString : "+newString);
System.out.println("capitalString : "+capitalString);
System.out.println("length Of oldString : "+lengthOfString+" karakter");
System.out.println("gabung : "+gabung);
}
}
run:
oldString : Have a nice day!
newString : H-ve - nice d-y!
capitalString : HAVE A NICE DAY!
length Of oldString : 16 karakter
gabung : Have a nice day! and always Fun
5
3.3 Instance Methods
As you are well aware, instance methods relate to some aspect of the
instantiated object. For example, the length of a string, the conversion of a
string toUpperCase characters, and so on.
SYNTAX
Method:
modifier(s) return-type method-name ( formal-parameter-list )
{
declarations
statements
}
The return-type identifies the type of the value that the method will return with
its return statement.
The syntax of the return statement is:
SYNTAX
Return Statement:
return expression;
6
Figure 3.2 A class may define constructors, instance, class, and helper methods
7
8
9