init Method in Java
In Java, the init method is a special method that can be defined in classes to perform initialization tasks. The init method is not specific to a particular interface or class like in the case of servlets, but rather it can be defined in any class as a regular method. The init method in Java is typically used to initialize the state of an object or perform setup tasks before the object can be used. It is commonly invoked after an object is created or instantiated.
The init method is called by the servlet container when the servlet is first loaded or initialized. It is invoked only once during the lifecycle of a servlet. The purpose of the init method is to perform any necessary setup or initialization tasks before the servlet can handle requests.
Example 1
In this above program, we have a Student class with two private member variables: name and roll number. The class has a default constructor, which automatically calls the init method. After calling the init method it will be going to display student information.
FileName: Student.java
public class Student { private String name; private int rollNo; private String branch; public Student () { init(); // Call the init() method to initialize the Student object } private void init() { name = "Sakshi Pawar"; // Set the default name to "Sakshi Pawar" rollNo = 25; // Set the default Roll to 25 branch= "CSE"; //set the branch as CSE System.out.println("Student initialized: " + name + ", " + rollNo + ", " + branch); // Print a message indicating the person has been initialized } public void displayInfo() { System.out.println("Name: " + name); // Print the name of Student System.out.println("Roll No: " + rollNo); // Print the Roll number of student System.out.println("Branch: " + branch); // Print the Branch of student } public static void main(String[] args) { Student student = new Student (); // Create a new Student object student.displayInfo(); // Display the Student’s information } }
Output
Student initialized: Sakshi Pawar, 25, CSE Name: Sakshi Pawar Roll No: 25 Branch: CSE
Example 2
In the above program, we have a Car class with a default constructor and an init method that takes make, model, and year as parameters. The main method creates an instance of the Car class, initializes it using the init() method, and then prints the details using the printDetails() method.
FileName: Car.java
public class Car { private String make; private String model; private int year; public Car() { // Default constructor } public void init(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } public void printDetails() { System.out.println("Make: " + make); // prints the maker of the car System.out.println("Model: " + model); // prints the model of the car System.out.println("Year: " + year); // prints the launching year of the car } public static void main(String[] args) { // Create a new instance of the Car class Car car = new Car(); // Initialize the car object using the init method car.init("Mahindra & Mahindra", "Thar SUV", 2023); // Print the details of the car car.printDetails(); } }
Output
Make: Mahindra & Mahindra Model: Thar SUV Year: 2023
Example 3
In the above program, I have added a JButton and a JTextField to the panel object. The button is created with the label "Click Me" and added to the panel using the add() method. Similarly, the text field is also added to the panel. we can further customize and add additional components as per our requirements within the init() method.
FileName: MyFrame.java
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class MyFrame extends JFrame { private JPanel panel; private JButton button; private JTextField textField; public MyFrame() { init(); } private void init() { // Set up the frame setTitle("My Swing Application"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create a panel panel = new JPanel(); // Create a button with label click me button = new JButton("Click Me"); panel.add(button); // Create a text field with size 20 textField = new JTextField(20); panel.add(textField); // Add the panel to the frame add(panel); // Pack the frame and make it visible to the end user pack(); setVisible(true); } public static void main(String[] args) { // main method // Create and display the frame MyFrame frame = new MyFrame(); } }
Output
