KIO
Kreative Ideen online

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
  • things the object does
    methodes

Things an object knows about itself are called instance variables.

They represent an object’s state (the data), and can have unique values for each object of that type.

Things an object can do are called 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.

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 the Button class to make dozens of different buttons, and each button might have its own color, size, shape, label, and so on.

[php] class Movie { String title; String genre; int rating; void playIt() { System.out.println(" Playing the movie"); } } public class MovieTestDrive { public static void main( String[] args) { Movie one = new Movie(); one.title = "Gone with the Stock"; one.genre = "Tragic"; one.rating = 1; one.playIt(); } } [/php]

Here’s an encapsulation starter rule of thumb (all standard disclaimers about rules of thumb are in effect): mark your instance variables private and provide public getters and setters for access control. For now, this approach will keep you safe.

Mark instance variables private.

Mark getters and setters public.

Leave a Reply

Your email address will not be published. Required fields are marked *