1620_lecture_14 - ITEC3620 Object-Based Programming Lecture...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
ITEC3620 Object-Based Programming Lecture 14 References II
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Using Classes What was necessary before using a class? Construction Random random = new Random(); Scanner scan = new Scanner(System.in);
Background image of page 2
Constructors Syntax has () () indicate a method Constructors are a special method that return a new instance of a class May also pass in parameters
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Constructors II Special method, special syntax <access modifier> Classname (<parameters>) No return type – must be instance of class Identifier must be same as class
Background image of page 4
Example public class Point { public int x; public int y; public Point (int x, int y) // parameters get assigned to instance variables }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example II Point corner = new Point (3, 5); corner 3 5 x y Point
Background image of page 6
Review Java does not know how to initialize your datatype You must use a constructor A constructor is a special method that has the same name as the class The constructor allocates space in memory for all the fields of the class Space allocation requires “new”
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Review II You must use the constructor before using your objects Point pointA; pointA.x = 0; // error Point pointB = new Point(); pointB.x = 0; // OK
Background image of page 8
What does the declaration do? Point pointA;
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 37

1620_lecture_14 - ITEC3620 Object-Based Programming Lecture...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online