ClassesAndObjects

ClassesAndObjects - Parameter counter is just getting assigned to itself Implementing Constructor Implementing Constructor Set start value for

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
public class Incrementor { /* Set start value for Incrementor counter */ public Incrementor(int startValue) { counter = startValue; } ... } Name of class is used as constructor name Constructor does not specify return type Responsible for initializing object It is called when an object is created Constructors /* Set start value for Incrementor counter */ public Incrementor(int startValue) { counter = startValue; } Implementing Constructor /* Set start value for Incrementor counter */ public Incrementor(int counter ) { counter = counter ; } Um, there’s a problem here.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: .. Parameter counter is just getting assigned to itself! Implementing Constructor Implementing Constructor /* Set start value for Incrementor counter */ public Incrementor(int counter) { this. counter = counter; } • this refers to receiver object • Automatically available to methods of a class /* Set start value for Incrementor counter */ public Incrementor(int startValue ) { counter = startValue ; } • Just give parameters distinct names from member variables • Preferred route (less thinking…) Implementing Constructor...
View Full Document

This note was uploaded on 02/18/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Ask a homework question - tutors are online