What are the fundamental things you need to think about when you design a Java class? What are the questions you need to ask yourself? If you could design a checklist to use when you’re designing a class, what would be on the checklist?
~~ Chapter 2 – page 33 (65 on pdf) ~~

When you design a class, think about the objects that will be created from that class type. Think about:

  • things the object knows (instance variables as another way of saying object)
  • things the object does (methods)

Things an object can do are called methods. When you design a class, you think about the data an object will need to know about itself, and you also design the methods that operate on that data. It’s common for an object to have methods that read or write the values of the instance variables. For example, Alarm objects have an instance variable to hold the alarmTime, and two methods for getting and setting the alarmTime.

So objects have instance variables and methods, but those instance variables and methods are designed as part of the class.
~~ Chapter 2 – page 34 (66) ~~

A class is not an object.
(but it’s used to construct them)

A class is a blueprint for an object. It tells the virtual machine how to make an object of that particular type. Each object made from that class can have its own values for the instance variables of that class. For example, you might use Button class to make dozens of different buttons, and each button might have its own color, size, shape, label, and so on.
~~Chapter 2 – page 35 (67)

Quick! Get out of main!

As long as you’re in main(), you’re not really in Objectville. It’s fine for a test program to run within the main method, but in true OO application, you need objects talking to other objects, as opposed to a static main() method creating and testing objects.

The two uses of main:

  • to test your real class
  • to launch/start your Java application

A real Java application is nothing but objects talking to other objects. In this case, talking means objects calling methods on one another.
~~ Chapter 2 – page 38(70)

Java takes out the Garbage

Each time an object is created in Java, it goes into an area of memory known as The Heap. All objects – no matter when, where, or how they’be created – live on the heap. But it’s not just any old memory heap; the Java heap is actually called the Garbage-Collectible Heap. When you create an object, Java allocates memory space on the heap according to how much that particular objects needs. An object with, say, 15 instance variables, will probably need more space than an object with only two instance variables. But what happens when you need to reclaim that space? How do you get an object out of the heap when you’re done with it?

Java manages that memory for you!. When the JVM can ‘see’ that an object can never be used again, that objects become eligible for garbage collection. And if you’re running low on memory, The Garbage Collector will run, throw out the unreachable objects, and free up the space, so that the space can be reused.  In later chapters you’ll learn more about how this works.
~~ Chapter 2 – page 40 (72) ~~

Advertisements