Skip to Main Content
Learn to Program, 2nd Edition
book

Learn to Program, 2nd Edition

by Chris Pine
March 2009
Intermediate to advanced content levelIntermediate to advanced
194 pages
4h
English
Pragmatic Bookshelf
Content preview from Learn to Program, 2nd Edition

new vs. initialize

That’s some pretty cool stuff we just covered. But the relationship between new and initialize is a bit subtle. And “subtle” may as well mean “confusing.” Just what is the deal?

The methods new and initialize work hand in hand.  You use new to create a new object, and initialize is then called automatically (if you defined it in your class). They pretty much happen at the same time. How do you keep them straight?

First, new is a method of the class, while initialize is a method of the instance. You use new to create the instance, and then initialize is automatically called on that instance. This means that the call to new must come first! Until you call new, there’s no instance to call initialize upon.

Second, you define ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learn to Program, 3rd Edition

Learn to Program, 3rd Edition

Chris Pine
The Book of Ruby

The Book of Ruby

Huw Collingbourne

Publisher Resources

ISBN: 9781680500172Errata