DEV Community

Daniel Reis
Daniel Reis

Posted on

1

Object in Java(behavior and states)

Object Definition

Object mean a one part for software bundle. Object share two characteristics: They all have state and behavior!

State

State represents an object caracteristic, two examples is:

  • Dog has name, color, breed, hungry( #dogstates ) like states and this states has any "primitive information"(Behavior transform primitive information into "dynamic information", of course exists states not dynamic but object characteristic, "Name", "Color", "Breed" not possible to have a behavior in this example, this informations are final object in Java)

  • Bicycles has current gear, current pedal cadence, current speed( #bicyclesstates )

Behavior

Behavior are a comportament only or add state to dynamicity, "How?". See this in the next examples:

  • Dog states " #dogstates" and the dogs behavior(comportament only) wagging tail, fetching, barking, behavior with state: hungryDog(!boolHungryDog = "x" || boolHungryDog = "x")
  • Bicycles states " #bicyclesstates ", all bicycles behavior depends bicycles states, see bicycles behavior now: Gear, Cadence, Speed all mentioned behavior depends bicycles states

Code:

EXAMPLE 1:

public class Dog {  
    final String nameDog = "Teo";  
    final String colorDog = "Black and white";  
    final String breedDog = "Shi-Tzu";  
    public Boolean boolHungryDog = false;  
    public String hungryDog = " ";  

    public Dog() {  
        if (!boolHungryDog) {  
            hungryDog = "Teo is hungry, feed him! >:(";  
        } else {  
            hungryDog = "Teo is not hungry, HAPPY :)";  
        }  
    }  

        public static void main(String args[]) {  
            Dog myDog = new Dog();  
            System.out.println("My dog " + myDog.nameDog + " has " +
            myDog.colorDog + " color" + " my dog the breed " + myDog.breedDog +
            " and " + myDog.hungryDog);  
    }  
}
Enter fullscreen mode Exit fullscreen mode

EXAMPLE 2:

public class Bicycles {
    public int gear = 0;
    public int cadence = 0;
    public int speed = 0;

    void changeCadence(int newValue){
        cadence = newValue;
    }
    void changeGear(int newValue){
    gear = newValue;
    }
    void speedUp(int increment){
        speed = speed + increment;
    }
    void applyBrakes(int decrement){
    speed = speed - decrement;
    }
void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}

class BicycleDemo {
    public static void main(String[] args) {

        Bicycle bike1 = new Bicycle();
        Bicycle bike2 = new Bicycle();

        bike1.changeCadence(50);
        bike1.speedUp(10);
        bike1.changeGear(2);
        bike1.printStates();

        bike2.changeCadence(50);
        bike2.speedUp(10);
        bike2.changeGear(2);
        bike2.changeCadence(40);
        bike2.speedUp(10);
        bike2.changeGear(3);
        bike2.printStates();
    }
}
Enter fullscreen mode Exit fullscreen mode

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more