Constructors in Java

Java Constructors:
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes:
Example:
public class MyClass {
int x;  // Create a class attribute
public MyClass() {
x = 5;  
}
public static void main(String[] args) {
MyClass myObj = new MyClass(); //constructor object
System.out.println(myObj.x); // Print the value of x
}
}
Types of Constructors:
  • Default constructor
  • No arg Constructor
  • Parameterized Constructor

Comments

Popular posts from this blog

Flutter : Introduction

How to release your Flutter app

Firebase Cloud Messaging