Classes, Objects and Memory
Make your own types — constructors, static, references on the stack and objects on the heap, strings and wrappers.
- Module 2 of 11
- Beginner
- 10 chapters
- 53 min
Classes are how Java code is organised, and knowing where objects live explains bugs that otherwise look like magic: shared references, null pointers and strings that never change.
What you will be able to do
- Design classes with constructors and static members
- Draw what sits on the stack and the heap
- Explain why strings are immutable and when to use StringBuilder
- Compare objects correctly with equals
Chapters
1. Classes and ObjectsA class is the blueprint: it defines a new type by saying what data each thing of that type has and what it can do.4 min2. Constructors and thisA constructor is special code that runs when an object is created with new, to set it up. It has the same name as the class3 min3. Instance vs StaticAn instance member (field or method) belongs to each object. It needs an object to be used: priya.addBonus(20).4 min4. Objects in MemoryObjects always live on the heap. A variable of a class type (Student priya) holds a reference — the address card7 min5. final and Immutabilityfinal on a variable means the variable can't be reassigned. For a reference, the reference is fixed — the object it points to3 min6. Strings, ProperlyString is immutable (Ch 5). Every method that seems to change a string — toUpperCase, replace, substring, + —6 min7. StringBuilderStringBuilder is a mutable sequence of characters: you change it in place. Call toString() at the end to get a String.3 min8. Wrapper Classes and AutoboxingEach primitive type has a wrapper class that holds one value as an object:5 min9. Equality and toString — = asks identity: "are these two references pointing to the same object?" (For primitives, it compares values.)3 min10. Packages and ImportsA package is a named group of related classes, matching a folder on disk. Names are usually a reversed web domain:2 min
Extras
The Whole StoryThe whole module retold as one story — no new facts, a fast revision of Classes, Objects and Memory.RevisionInterview QuestionsReal interview questions on Classes, Objects and Memory, answered the way you would say them out loud.Interview prepQuick ReferenceGlossary and cheat sheet for Classes, Objects and Memory — the page to reread before an interview.Reference