Académique Documents
Professionnel Documents
Culture Documents
Creating Objects
The System.out object has already been created, and
already exists for your use in a Java program.
Typically, you must create an object before you can
use it. In order to create an object, a class must
exist which describes this type of object . To use
this class, you must be familiar with the creation
methods (constructors) it provides.
A constructor method always has the same name as
the class.
www.java.sun.com provides
class specs (see API specs)
Object Variables
Declare
Rectangle crispyCrunchy;
Declare and initialize:
Rectangle cerealBox = new Rectangle(5, 10, 20, 30);
Once you have an object, you can apply methods:
cerealBox.translate(15, 25);
crispyCrunchy.translate(10,5); //error !!
Share objects:
r = cerealBox;
import java.awt.Rectangle;
Purpose:
To import a class from a package for use in a
program
File MoveTest.java
import java.awt.Rectangle;
public class MoveTest {
public static void main(String[] args){
// declare and create the rectangle
Rectangle cerealBox = new
Rectangle(5,10,20,30);
// move the rectangle
cerealBox.translate(15, 25);
// print the moved rectangle
System.out.println(cerealBox);
}
}
Call:
myrec.contains(6,7)