In Java, an object is an instance of a class, which is a blueprint for creating objects. A class defines the properties (fields) and behaviors (methods) of an object.
Declaring a Class:
To declare a class in Java, you use the class
keyword, followed by the name of the class. For example:
public class Person { // class code goes here }
Declaring Fields:
A class can have one or more fields, which are variables that hold data. Fields are declared inside the class but outside of any methods. For example:
public class Person { private String name; private int age; }
Declaring Methods:
A class can also have one or more methods, which are blocks of code that perform a specific task. Methods are declared inside the class, after the fields. For example, let’s say you have a Person
class with the following fields and constructor:
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
Creating Objects:
In Java, you create objects by instantiating classes using the new
operator. The general syntax for creating an object is as follows:
ClassName objectName = new ClassName();
Here, ClassName
is the name of the class that you want to create an object from, and objectName
is the name of the object that you are creating.
To create a Person
object, you would do the following:
Person person1 = new Person("Alice", 25);
Here, person1
is the name of the object that you are creating, and new Person("Alice", 25)
is the constructor call that creates the object. The constructor takes two arguments, "Alice"
and 25
, which are used to set the name
and age
fields of the object, respectively.
You can create as many objects as you want from a single class:
Person person2 = new Person("Bob", 30); Person person3 = new Person("Charlie", 35);
Here, person2
and person3
are two more objects that are created from the Person
class.
Once you have created an object, you can access its fields and methods using the dot notation:
Accessing Fields and Methods:
You can access the fields and methods of an object using the dot notation. For example:
System.out.println(person1.getName()); // Output: Alice System.out.println(person2.getAge()); // Output: 30
Inheritance:
Java also supports inheritance, which is a mechanism that allows you to create a new class based on an existing class. The new class inherits all the fields and methods of the existing class and can add new fields and methods of its own. For example:
public class MySubclass extends MyClass { String yetAnotherField; public void yetAnotherMethod() { // method code goes here } }
The extends
keyword indicates that MySubclass
is a subclass of MyClass
.
In summary, to create an object in Java, you need to:
- Declare a class with fields and methods
- Instantiate the class using the
new
operator - Optionally, pass arguments to the constructor to set the object’s initial state
- Access the object’s fields and methods using the dot notation