Object Classes

Java objects

  • In Java, objects are essentially instances of classes. They represent real-world entities or concepts and contain both data and behaviors, facilitating organized and modular software design.

Common Types of Java Classes

In Java, classes are the fundamental building blocks of Object-Oriented Programming (OOP). They define the structure and behavior of objects, serving as templates or blueprints from which objects are created. Java programs typically consist of multiple classes, each designed to fulfill a specific role. One Java class that you should be familiar with is:

  1. Utility Classes: Utility classes contain static methods that provide common utility functions. These classes are not meant to be instantiated, and they often involve operations such as mathematical calculations, string manipulation, or date formatting. The Math class is an example of a utility class.

    • Java Arrays Class
    • Java HashMap Class
    • Java LinkedList Class

```java import java.util.Arrays;

public class ArraySortExample { public static void main(String[] args) { int[] numbers = {5, 2, 9, 1, 5, 6}; Arrays.sort(numbers); System.out.println(Arrays.toString(numbers)); } } String[] words = {“main”}; ArraySortExample.main(words); Java Objects An object in Java is a basic unit of Object-Oriented Programming and represents real-life entities. Objects are the instances of a class that are created to use the attributes and methods of a class. A typical Java program creates many objects, which, as you know, interact by invoking methods. An object consists of:

State: It is represented by attributes of an object. It also reflects the properties of an object. Behavior: It is represented by the methods of an object. It also reflects the response of an object with other objects. Declaring Objects When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state, are unique for each object. A single class may have any number of instances.

As we declare variables like (type name;). This notifies the compiler that we will use the name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variables, the type must be strictly a concrete class name. In general, we can’t create objects of an abstract class or an interface.

Initializing a Java Object The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor.

java Copy code public class Dog { public String Name; public int Age;

public void Bark(int times) {
    for (int i = 0; i < times; i++) {
        System.out.println("Bark");
    }
}

public static void main(String[] args) {
    Dog dog = new Dog();

    dog.Bark(5);
} } Dog.main(null); Calling a non-void method It's possible to call a non-void method on an object so that it returns a value, the type of which is specified with the method.

java Copy code public class Dog { public String Name; public int Age;

public Dog(String name, int age) {
    Name = name;
    Age = age;
}

public String getName() {
    return Name;
}

public static void main(String[] args) {
    Dog d = new Dog("Larry", 4);
    System.out.println(d.getName());
} } Popcorn Hacks Modify the code below (the same code as above) to demonstrate one of the following:

An additional Non-void method Multiple uses of the same Non-void method on different objects java Copy code public class Dog { public String Name; public int Age;

public Dog(String name, int age) {
    Name = name;
    Age = age;
}

public String getName() {
    return Name;
}

public int getAge() {
    return Age;
}

public static void main(String[] args) {
    Dog d = new Dog("Larry", 4);
    System.out.println(d.getName());
    System.out.println(d.getAge());
} } String Objects String objects are built-in objects in Java that you're probably pretty familiar with, but it also functions like a class, with the following properties:

String objects can be initialized with the new keyword Method that acts upon String objects can’t change the String itself; it is immutable. String objects can be concatenated using the + operator, creating a new string that is the combination of the other two strings Escape sequences can be used to print characters that are otherwise unavailable, using the “" (backslash) character. java Copy code public class StringObjects { public static void main(String[] args) { String firstName = “John”; String lastName = “Doe”; String fullName = firstName + “ “ + lastName; System.out.println(fullName); } } StringObjects.main(null); Custom Classes You can create custom classes in Java to define your own object types. These classes can have attributes (fields) and methods that define the behavior of objects created from them. Here’s a simple example:

java Copy code public class Person { private String name; private int age;

public Person(String name, int age) {
    this.name = name;
    this.age = age;
}

public void sayHello() {
    System.out.println("Hello, my name is " + name + " and I'm " + age + " years old.");
}

public static void main(String[] args) {
    Person person1 = new Person("Alice", 30);
    Person person2 = new Person("Bob", 25);

    person1.sayHello();
    person2.sayHello();
} } Person.main(null); Conclusion Java objects and classes are essential elements of Object-Oriented Programming. Objects represent real-world entities, and classes define their structure and behavior. You can create custom classes to model your own object types with specific attributes and methods. Understanding how to work with objects and classes is fundamental to Java development.