Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference
Creating objects in Java involves understanding several key concepts: classes, objects, constructors, and the new keyword. Let’s break these down step by step.

1. Classes

A class is a blueprint for objects. It defines a type and contains fields (variables) and methods to define the behavior of objects created from the class.
				
					public class Car {
    // Fields
    String color;
    String model;
    int year;

    // Constructor
    public Car(String color, String model, int year) {
        this.color = color;
        this.model = model;
        this.year = year;
    }

    // Methods
    public void displayDetails() {
        System.out.println("Car model: " + model);
        System.out.println("Car color: " + color);
        System.out.println("Car year: " + year);
    }
}

				
			

2. Creating Objects

To create an object of a class, you need to use the new keyword followed by the class constructor.
				
					public class Main {
    public static void main(String[] args) {
        // Creating an object of the Car class
        Car myCar = new Car("Red", "Toyota", 2021);

        // Accessing methods
        myCar.displayDetails();
    }
}

				
			

Detailed Steps:

  1. Define the Class: Create a class with fields and methods. Optionally, include a constructor to initialize objects.
  2. Create an Object: Use the new keyword followed by the class constructor to create an object.
  3. Access Members: Use the dot . operator to access fields and methods of the object.

Constructors

A constructor is a special method that is called when an object is instantiated. It initializes the object.

  • Default Constructor: If no constructor is defined, Java provides a default constructor.
  • Parameterized Constructor: A constructor with parameters to initialize fields.
				
					public class Car {
    String color;
    String model;
    int year;

    // Default Constructor
    public Car() {
        this.color = "Unknown";
        this.model = "Unknown";
        this.year = 0;
    }

    // Parameterized Constructor
    public Car(String color, String model, int year) {
        this.color = color;
        this.model = model;
        this.year = year;
    }
}

				
			

Instantiating Objects with Constructors

  • Using the default constructor:
				
					Car myDefaultCar = new Car();
myDefaultCar.displayDetails();

				
			
  • Using the parameterized constructor:
				
					Car myParamCar = new Car("Blue", "Honda", 2022);
myParamCar.displayDetails();

				
			
  • Access Modifiers

    Access modifiers control the visibility of class members.

    • public: Accessible from any other class.
    • private: Accessible only within the defined class.
    • protected: Accessible within the same package and subclasses.
    • default (no modifier): Accessible only within the same package.
				
					public class Car {
    private String color;
    private String model;
    private int year;

    // Getter and Setter for color
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    // Other methods and constructors...
}

				
			

Example Program

Here’s a full example putting everything together:

				
					public class Car {
    private String color;
    private String model;
    private int year;

    // Default Constructor
    public Car() {
        this.color = "Unknown";
        this.model = "Unknown";
        this.year = 0;
    }

    // Parameterized Constructor
    public Car(String color, String model, int year) {
        this.color = color;
        this.model = model;
        this.year = year;
    }

    // Getter and Setter methods
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    // Method to display details
    public void displayDetails() {
        System.out.println("Car model: " + model);
        System.out.println("Car color: " + color);
        System.out.println("Car year: " + year);
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an object using the default constructor
        Car defaultCar = new Car();
        defaultCar.displayDetails();

        // Creating an object using the parameterized constructor
        Car paramCar = new Car("Red", "Toyota", 2021);
        paramCar.displayDetails();

        // Using getter and setter methods
        paramCar.setColor("Blue");
        System.out.println("Updated color: " + paramCar.getColor());
    }
}

				
			

This example demonstrates the fundamental principles of creating and using objects in Java, including class definition, constructors, access modifiers, and methods.

Scroll to Top