Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Create Objects (Explicitly And Implicitly)

In Java, objects are instances of a class, and they represent real-world entities. There are two primary ways to create objects in Java: explicitly and implicitly. In this tutorial, we will cover both methods of creating objects.

  • Creating Objects Explicitly

Explicitly creating objects in Java involves using the new keyword followed by the constructor of the class. The constructor is a special method that initializes an object's attributes and sets its initial state.

a. Using the default constructor:

The default constructor is a constructor with no arguments, which the Java compiler automatically generates if no other constructors are defined. Here's an example of creating an object explicitly using the default constructor:

public class MyClass {
    public String myAttribute;
}

public class Main {
    public static void main(String[] args) {
        MyClass obj1 = new MyClass();
        obj1.myAttribute = "Hello, World!";
        System.out.println(obj1.myAttribute); // Output: Hello, World!
    }
}

b. Using a custom constructor:

You can also define your own constructors with parameters to initialize an object's attributes when creating it explicitly:

public class MyClass {
    public String myAttribute;

    public MyClass(String myAttribute) {
        this.myAttribute = myAttribute;
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass obj2 = new MyClass("Hello, Java!");
        System.out.println(obj2.myAttribute); // Output: Hello, Java!
    }
}
  • Creating Objects Implicitly

Implicitly creating objects in Java involves using built-in Java methods or constructors of other classes that return an instance of the desired class.

a. Using a factory method:

Factory methods are static methods that return an instance of the class. In this example, we're using LocalDate.now() from the java.time.LocalDate class, which returns the current date as a LocalDate object:

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate currentDate = LocalDate.now();
        System.out.println(currentDate); // Output: YYYY-MM-DD (current date)
    }
}

b. Using a method that returns an object:

In this example, we're using the parse() method from the java.time.LocalDate class, which takes a date string and returns a LocalDate object:

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate dateFromString = LocalDate.parse("2023-05-01");
        System.out.println(dateFromString); // Output: 2023-05-01
    }
}

In summary, you can create objects in Java explicitly and implicitly. Explicitly creating objects involves using the new keyword and the class constructor, while implicitly creating objects involves using factory methods, built-in Java methods, or methods of other classes that return an instance of the desired class.

  1. Implicit object creation in Java

    Implicit object creation refers to objects that are created automatically, such as the this and super objects in non-static and superclass contexts.

    public class MyClass {
        // Implicit object creation (this)
        private int value;
    
        public void setValue(int value) {
            this.value = value;
        }
    }
    
  2. Using the 'new' keyword to create objects in Java

    The new keyword is used to explicitly create objects in Java. Example:

    public class Car {
        String model;
    
        public static void main(String[] args) {
            // Using 'new' keyword to create an object
            Car myCar = new Car();
            myCar.model = "Toyota";
        }
    }
    
  3. Object initialization in Java

    Objects can be initialized using constructors or setter methods. Example:

    public class Book {
        String title;
    
        // Constructor for object initialization
        public Book(String title) {
            this.title = title;
        }
    
        public static void main(String[] args) {
            // Initializing an object using a constructor
            Book myBook = new Book("Java Programming");
        }
    }
    
  4. Creating objects without 'new' keyword in Java

    Objects can also be created without using the new keyword, such as when invoking static factory methods. Example:

    public class MyClass {
        private MyClass() {
            // Private constructor
        }
    
        // Static factory method for object creation
        public static MyClass createInstance() {
            return new MyClass();
        }
    }
    
  5. Java default constructor and object creation

    If a class doesn't explicitly define a constructor, Java provides a default constructor with no parameters. Example:

    public class Person {
        String name;
    
        public static void main(String[] args) {
            // Using the default constructor
            Person person = new Person();
            person.name = "John";
        }
    }
    
  6. Initializing objects with constructor parameters in Java

    Constructors can have parameters for initializing objects with specific values. Example:

    public class Circle {
        private double radius;
    
        // Constructor with parameter
        public Circle(double radius) {
            this.radius = radius;
        }
    
        public static void main(String[] args) {
            // Initializing an object with a constructor parameter
            Circle myCircle = new Circle(5.0);
        }
    }
    
  7. Object creation in Java with constructor overloading

    Constructor overloading allows a class to have multiple constructors with different parameters. Example:

    public class Rectangle {
        private int length;
        private int width;
    
        // Default constructor
        public Rectangle() {
            // Initialization code
        }
    
        // Parameterized constructor
        public Rectangle(int length, int width) {
            this.length = length;
            this.width = width;
        }
    }
    
  8. Implicit object creation in Java frameworks

    In Java frameworks like Spring or Java EE, objects are often implicitly created and managed by the framework's container. For example, in Spring, you can define beans, and the Spring container will handle their instantiation.

    @Component // Indicates that this class is a Spring component (bean)
    public class MyBean {
        // Bean properties and methods
    }