Java's class is a collection of objects with similar attributes. An object is generated using a template or blueprint. The class cannot be tangible, as it is a logical entity. A class in Java can contain:
Fields
Methods
Constructors
Blocks
Nested class and interface
Java Class Properties
Class is not an actual object.
It is simply a template, blueprint, or prototype from what objects are built.
Class does not require memory.
A class consists of variables of various data types as well as methods.
Java Class Components for class declaration
Class declarations can generally include the following components, in this order:
Modifiers
Class keyword
Class name
Superclass(if any)
Interfaces(if any)
Body
What are Objects in Java?
An object is a stateful entity with behavior, such as a chair or a bike. It may be physical or logical (tangible and intangible).
Characteristics of Objects in Java
An object has 3 characteristics:
State
Behavior
Identity
Different ways to create objects in Java
Using the new keyword
Using Class.forName() method
Using clone() method
Using object deserialization
Using Factory Method
Using Dependency Injection
Java's Anonymous Objects
Anonymous objects, which are created without being stored in a reference variable, are used for direct method calls, which are common in many libraries, and are destroyed after method execution.