Please enable JavaScript to use CodeHS

CodeHS Glossary


Class Java

A class is a template, or a blueprint, from which Java objects are created. All Java programs start with a class. A class provides the instructions for how an object works. It is a blueprint for the object's state and behavior. An object is *constructed* from a class. A class is like the blueprint, or the plan. for a building, it provides all the information necessary to construct a building. An object is like the actual building that ends up being constructed from the blueprint. You can have several different objects that are constructed from the same class. This is just like having several different buildings that all are constructed from the same blueprint. For example, we can have a class called `Rectangle`, which defines how Rectangle objects should work. It provides the template for all Rectangle objects to be created from. The Rectangle class might look like this: ``` class Rectangle { int width; int height; public Rectangle(int w, int h) { width = w; height = h; } } ``` This is saying that all Rectangles should be made up of two ints: a width and a height. Then we can create several different Rectangle objects like this: ``` // Creates a Rectangle object with a width of 10 and a height of 5 Rectangle rect1 = new Rectangle(10, 5); // Creates a different Rectangle object with a width of 20 and a height of 2 Rectangle rect2 = new Rectangle(20, 2); ``` Classes and objects are the foundation of Object Oriented Programming.