Entri Blog
No Result
View All Result
Friday, July 1, 2022
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Other Govt. Exam
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET
FREE GK TEST: SIGNUP NOW
Entri Blog
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Other Govt. Exam
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET
No Result
View All Result
Entri Blog
Free GK Test
banner top article banner top article
Home Articles

Constructors In Java- An Overview for Beginners

by Ayesha Surayya
May 16, 2022
in Articles, Java Programming
Constructors In Java- An Overview for Beginners
Share on FacebookShare on WhatsAppShare on Telegram

Table of Contents

  • How Constructors are Different From Methods in Java? 
  • Need of Constructor
  • When is a Constructor called?
  • The rules for writing constructors 
  • Types of Constructors in Java
  • Default Constructor

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.

In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method which is used to initialize the object. Every time an object is created using the new() keyword, at least one constructor is called.

To know more about Java, Download Entri App!

How Constructors are Different From Methods in Java? 

  • Constructors must have the same name as the class within which it is defined while it is not necessary for the method in Java.
  • Constructors do not return any type while method(s) have the return type or voidif does not return any value.
  • Constructors are called only once at the time of Object creation while method(s) can be called any number of times.

Need of Constructor

Think of a Box. If we talk about a box class then it will have some class variables (say length, breadth, and height). But when it comes to creating its object(i.e Box will now exist in the computer’s memory), then can a box be there with no value defined for its dimensions. The answer is no.
So constructors are used to assigning values to the class variables at the time of object creation, either explicitly done by the programmer or by Java itself (default constructor).

Grab Latest Study Materials! Register Here!

When is a Constructor called?

Each time an object is created using a new() keyword, at least one constructor (it could be the default constructor) is invoked to assign initial values to the data members of the same class.

The rules for writing constructors 

  • Constructor(s) of a class must have the same name as the class name in which it resides.
  • A constructor in Java can not be abstract, final, static, or Synchronized.
  • Access modifiers can be used in constructor declaration to control its access i.e which other class can call the constructor.

Types of Constructors in Java

Now is the correct time to discuss types of the constructor, so primarily there are two types of constructors in java:

  • No-argument constructor
  • Parameterized Constructor
  • No-argument constructor

A constructor that has no parameter is known as the default constructor. If we don’t define a constructor in a class, then the compiler creates a default constructor(with no arguments) for the class. And if we write a constructor with arguments or no-arguments then the compiler does not create a default constructor.

  • Parameterized Constructor

A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with our own values, then use a parameterized constructor.

Grab Latest Study Materials! Register Here!

Default Constructor

The default constructor in Java initializes the data members of the class to their default values such as 0 for int, 0.0 for double etc. This constructor is implemented by default by the Java compiler if there is no explicit constructor implemented by the user for the class.

If you observe the following example, we are not providing any constructor to it.

public class Sample {

int num;

public static void main(String args[]){

System.out.println(new Sample().num);

}

}

If you compile and run the above program the default constructor initializes the integer variable num with 0 and, you will get 0 as result.

Why is it important to choose Entri?

  • Excellent online platform for all the Competitive Exams.
  • Provides updated materials created by the Entri Experts.
  • Entri provides a best platform with full- length mock tests including previous year question papers.
  • You can download the app for free and join the required classes.
  • Entri wishes you all the best for your examinations and future endeavours.

To know more about Java, Download Entri App!

“YOU DON’T HAVE TO BE GREAT TO START, BUT YOU HAVE TO START TO BE GREAT.”

 

Share61SendShare
Ayesha Surayya

Ayesha Surayya

Related Posts

Ocean Waves - Types and Characteristics
Articles

Ocean waves – Type and Characteristics

June 29, 2022
UPPSC BEO Answer key 2022 - Download PDF
Articles

UPPSC BEO Answer key 2022 – Download PDF

June 29, 2022
ICF Railway Recruitment 2022
Articles

ICF Railway Recruitment 2022: Apply for 876 Apprentice vacancies

June 29, 2022
Next Post
What are the Advantages of Speaking in English Fluently?

What are the Advantages of Speaking in English Fluently?

Discussion about this post

Latest Posts

  • How the Fashion Industry is Using Data Science?
  • Weekly English Vocabulary Based on The Hindu Editorial 2022 July 1
  • UPPSC BEO Salary 2022 – In Hand Salary, Pay Scale, Allowances
  • Getter and Setter in Java: A Complete Guide
  • KVS PRT Vacancy 2022

Trending Posts

  • states of india and their capitals and languages

    List of 28 States of India and their Capitals and Languages

    147010 shares
    Share 58801 Tweet 36751
  • List of Government Banks in India 2022: All you need to know

    58349 shares
    Share 23340 Tweet 14587
  • TNPSC Group 2 Posts and Salary Details 2022

    36960 shares
    Share 14784 Tweet 9240
  • New Map of India with States and Capitals 2022

    27856 shares
    Share 11142 Tweet 6964
  • Kerala Devaswom Board LDC Syllabus 2022 – Download PDF

    595 shares
    Share 238 Tweet 149

Company

  • Become a teacher
  • Login to Entri Web

Quick Links

  • Articles
  • Videos
  • Entri Daily Quiz Practice
  • Current Affairs & GK
  • News Capsule – eBook
  • Preparation Tips
  • Kerala PSC Gold
  • Entri Skilling

Popular Exam

  • IBPS Exam
  • SBI Exam
  • Railway RRB Exam
  • Kerala PSC
  • Tamil Nadu PSC
  • Telangana PSC
  • Andhra Pradesh PSC
  • MPPSC
  • UPPSC
  • Karnataka PSC
  • Staff Selection Commission Exam

© 2021 Entri.app - Privacy Policy | Terms of Service

No Result
View All Result
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Other Govt. Exam
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET

© 2021 Entri.app - Privacy Policy | Terms of Service

Try After Few Days!
30% OFF
Next time
Next time
Almost!
70% OFF
20% OFF
Next Time
Next time
Almost!
40% OFF
60% OFF
Unlucky
Spin the Wheel to Win FREE PREP on our courses! PSC, SSC, RRB, Banking, TET
Enter your email address and spin the wheel. This is your chance to win amazing discounts!
Our in-house rules:
  • One game per user
  • Cheaters will be disqualified.
  • Offer applicable for 1st time ENTRI users!