Programming Using Java: Java Classes and Objects: A Preview
Programming Using Java: Java Classes and Objects: A Preview
go = new JButton("Go");
Constructors (cont’d)
Call them using the new operator:
Point3D
point = 5,10,20
Point3D
point =
15,10,20 Point3D
point =
point2 = 20,35,20
point2 =
null references
Class X Class Y
private method
Methods (cont’d)
You can call methods with specific arguments:
g.drawRect (75, 25, 150, 50);
g.drawString ("Welcome", 120, 50);
To define a method:
– decide between public and private (usually public)
– give it a name
– specify the types of arguments (formal
parameters) and give them names
– specify the method’s return type or chose void
– write the method’s code
Methods (cont’d)
A method is always defined inside a class.
A method returns a value of the specified type
unless it is declared void; the return type can
be any primitive data type or a class type.
A method’s arguments can be of any primitive
data types or class types.
Empty parentheses indicate that
a method takes no arguments.
– synchronized
• Synchronization method
• Control the thread so that only on thread can
always access the target
– native
• To use the implementation written in other
programming languages such as C language
Parameter
Parameter Passing
– Formal parameter
– Actual parameter
void parameterPass(int i, Fraction f) {
// ...
}
Call by reference
main method