ClassesAndObjects

ClassesAndObjects - counter is just getting assigned to...

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

View Full Document Right Arrow Icon
public class Incrementor { public Incrementor(int startValue) { counter = startValue; } public Incrementor() { counter = 1; } ... } Name of class is used as constructor name Constructor does not specify return type Responsible for initializing object It is called when object is created ( new called) 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. .. Parameter
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 10/25/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