Java Constructor.

java-constructor.

Date : 10-April-2025

Constructor

In java methods are typical one and we wrote every code in a method. That method are varies in nature.

Some methods works same in all time, not the inner content of the method, before executing the methods content, the methods all are working under a same priciple except some methods.

one of those except methods is Constructor. Constructor is a special type of method which is only invoked by an instance at that time of its creation. Constructor is only invoked at the time of object creation. Instance of class is the only invoker of a Constructor.

Without object we cannot able to call any constructor.

Rules to define a constructor.

  1. A constructor name must be same as its classname.
  2. A constructor must not have any method return type.
  3. A constructor can be overloaded.
  4. If no constructor is defined in the class, a default constructor will be assigned to it by JVM. Note: The default constructor is a no argument constructor.

e.g,

class ConstructorSample{

ConstructorSample(){
System.out.println(“This is a constructor”);
}

public static void main(String[] args){

ConstructorSample cs = new ConstructorSample();
}
}

output:

This is a contructor.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
navigating-common-hosting-pitfalls:-strategies-for-freelancers-and-e-commerce-pros

Navigating Common Hosting Pitfalls: Strategies for Freelancers and E-Commerce Pros

Next Post
russian-news-opinion-mining:-new-dataset-&-extraction-methods

Russian News Opinion Mining: New Dataset & Extraction Methods

Related Posts