0% found this document useful (0 votes)
37 views2 pages

Understanding Java Constructors and Methods

Uploaded by

contato.rcs7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views2 pages

Understanding Java Constructors and Methods

Uploaded by

contato.rcs7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

74 ▪ Quick Java

Note: Private fields are private to the class, not to the object.
Objects have no privacy from other objects of the same type. For
example, code in the Password class (see section 4.1.1) can
access not only [Link], but also [Link], ja­
[Link], and so on.

The type of a field can be the name of any primitive type (int, double,
etc.) or the name of any class type (String, Exception, Password, etc.).

By convention, each fieldName should begin with a lowercase letter.

The expression, if present, is typically just a simple value, though it can


be an expression involving any fields defined above it.

Examples:

String name;
double length = 22.75;
private int count = 0;

4.1.5 Constructors I
The purpose of a constructor is to create an object in a valid state. No
other work should be done in a constructor.

A constructor looks a lot like a method, but the returnType and


methodName are replaced by the ClassName. The syntax is

/** documentation comment */


access ClassName(parameterList) {
declarations
statements
}

In a constructor, the keyword this refers to the object being constructed.

A constructor typically doesn’t declare any local variables; all it does is


use its parameters to assign values to the instance variables. For con­
venience, it is common for the parameters to have the same names as the

More books: [Link]


The “Outer Language” of Java ▪ 75

instance variables, so a constructor may consist of little more than some


assignments of the form [Link] = name;.

For example, you might have a class Customer that starts out like this:

public class Customer {


String name;
String address;
double amountOwed = 0;

/** Here is the constructor */


Customer(String name, String address) {
[Link] = name;
[Link] = address;
}
}

Here, [Link] and [Link] refer to the instance variables,


while name and address refer to the parameters.

The constructor is called with the keyword new. For example,

Customer c = new Customer("Jane", "jane@aol");

The newly created object is returned as the value of the call to the
constructor. No return statement is necessary in a constructor.

4.1.6 Defining Methods


A method is like a function, except that it belongs to a class. Like a
function, it takes parameters and may return a value. The syntax is

/** documentation comment */


access returnType methodName(parameterList) {
declarations
statements
}

With minor exceptions, methods contain all the executable code of a


program.

More books: [Link]

You might also like