Behavior and Attributes

Every class you write in Java has two basic features: attributes and behavior. In this section you’ll learn about each one as it applies to a theoretical simple class called Motorcycle. To finish up this section, you’ll create the Java code to implement a representation of a motorcycle.

Attributes

Attributes are the individual things that differentiate one object from another and determine the appearance, state, or other qualities of that object. Let’s create a theoretical class called Motorcycle. A motorcycle class
might include the following attributes and have these typical values:
Color: red, green, silver, brown
Style: cruiser, sport bike, standard
Make: Honda, BMW, Bultaco

Attributes of an object can also include information about its state; for example, you could have features for engine condition (off or on) or current gear selected.

Attributes are defined in classes by variables. Those variables’ types and names are defined in the class, and each object can have its own values for those variables. Because each instance of a class can have different values for its variables, these variables are often called instance variables.

New Term
An instance variable defines the attributes of the object. Instance variables’ types and names are defined in the class, but their values are set and changed in the object.

Instance variables may be initially set when an object is created and stay constant throughout the life of the object, or they may be able to change at will as the program runs. Change the value of the variable, and you
change an object’s attributes.

In addition to instance variables, there are also class variables, which apply to the class itself and to all its instances. Unlike instance variables, whose values are stored in the instance, class variables’ values are stored in
the class itself. You’ll learn about class variables later on this week and more specifics about instance variables tomorrow.

In object-oriented programming (OOP) with Java, attributes are the properties or characteristics of objects. They represent the state of an object and define its structure. In Java, attributes are typically implemented as fields or member variables within a class. Here’s how attributes are utilized in OOP with Java:

  1. Declaration: Attributes are declared within a class using variable declaration syntax. They specify the type of data the attribute can hold and can optionally be initialized with a value.

				
					public class MyClass {
    // Attribute declaration
    private int myAttribute;
}

				
			

2. Access Modifiers: Attributes can have different access modifiers like public, private, protected, or package-private (no modifier). These modifiers control the visibility and accessibility of attributes from other classes.

				
					public class MyClass {
    // Private attribute
    private int myAttribute;
}

				
			

3. Initialization: Attributes can be initialized when declared or later in constructors or methods.

				
					public class MyClass {
    // Attribute initialized during declaration
    private int myAttribute = 10;
}

				
			

4. Getter and Setter Methods: Encapsulation is often used in Java to ensure proper access to attributes. Getter and setter methods provide controlled access to attribute values, allowing for better encapsulation and flexibility in modifying attribute values.

				
					public class MyClass {
    private int myAttribute;

    // Getter method
    public int getMyAttribute() {
        return myAttribute;
    }

    // Setter method
    public void setMyAttribute(int value) {
        this.myAttribute = value;
    }
}

				
			

5. Static Attributes: Attributes can also be declared as static, which means they belong to the class itself rather than to instances of the class.

				
					public class MyClass {
    // Static attribute
    private static int count = 0;
}

				
			

6. Final Attributes: Attributes can be declared as final, meaning they cannot be changed once initialized.

				
					public class MyClass {
    // Final attribute
    private final int constant = 100;
}

				
			

Attributes are fundamental to OOP in Java, as they represent the data associated with objects and define their behavior. Proper use of attributes, along with encapsulation and access control, leads to robust and maintainable Java code.

Behavior

A class’s behavior determines how an instance of that class operates; for example, how it will “react” if asked to do something by another class or object or if its internal state changes. Behavior is the only way objects can
do anything to themselves or have anything done to them. For example, to go back to the theoretical Motorcycle class, here are some behaviors that the Motorcycle class might have:
Start the engine
Stop the engine
Speed up
Change gear
Stall

To define an object’s behavior, you create methods, a set of Java statements that accomplish some task. Methods look and behave just like functions in other languages but are defined and accessible solely inside a class.
Java does not have functions defined outside classes (as C++ does).

New Term
Methods are functions defined inside classes that operate on instances of those classes.

While methods can be used solely to operate on an individual object, methods are also used between objects to communicate with each other. A class or an object can call methods in another class or object to
communicate changes in the environment or to ask that object to change its state.

Just as there are instance and class variables, there are also instance and class methods. Instance methods (which are so common that they’re usually just called methods) apply and operate on an instance of a class; class
methods apply and operate on the class itself. You’ll learn more about class methods later on this week.

Scroll to Top