Java – Constructors

Java - Constructors

This topic is about Java – Constructors.

A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type.

Typically, you will use a constructor to give initial values to the instance variables defined by the class, or to perform any other start-up procedures required to create a fully formed object.

All classes have constructors, whether you define one or not, because Java automatically provides a default constructor that initializes all member variables to zero. However, once you define your own constructor, the default constructor is no longer used.

Syntax

Following is the syntax of a constructor −

class ClassName {
   ClassName() {
   }
}

Java allows two types of constructors namely −

  • No argument Constructors
  • Parameterized Constructors

No argument Constructors

As the name specifies the no argument constructors of Java does not accept any parameters instead, using these constructors the instance variables of a method will be initialized with fixed values for all objects.

Example

Public class MyClass {
   Int num;
   MyClass() {
      num = 100;
   }
}

You would call constructor to initialize objects as follows

public class ConsDemo {
   public static void main(String args[]) {
      MyClass t1 = new MyClass();
      MyClass t2 = new MyClass();
      System.out.println(t1.num + " " + t2.num);
   }
}

This would produce the following result

100 100

Parameterized Constructors

Most often, you will need a constructor that accepts one or more parameters. Parameters are added to a constructor in the same way that they are added to a method, just declare them inside the parentheses after the constructor’s name.

Example

Here is a simple example that uses a constructor −

// A simple constructor.
class MyClass {
   int x;
   
   // Following is the constructor
   MyClass(int i ) {
      x = i;
   }
}

You would call constructor to initialize objects as follows −

public class ConsDemo {
   public static void main(String args[]) {
      MyClass t1 = new MyClass( 10 );
      MyClass t2 = new MyClass( 20 );
      System.out.println(t1.x + " " + t2.x);
   }
}

This would produce the following result −

10 20

In this topic we learned about Java – Constructors. To know more, Click Here.

This Post Has 31 Comments

  1. This is a great tip especially to those new to the blogosphere.Short but very accurate info… Appreciate your sharingthis one. A must read post!

  2. buy shares

    amoxicillin clav augmentin suspension augmentin vs amoxicillin

  3. buy stocks

    Rxkvgc – help with science homework Frslww ygzkxz

  4. index trading

    What a material of un-ambiguity and preserveness ofvaluable experience regarding unexpected emotions.

  5. Excellent way of explaining, and fastidious post to get data concerning my presentation subject matter, whichi am going to deliver in school.Also visit my blog :: Gorges De Soleil Cream Ingredients

  6. Awesome! Its actually remarkable post, I have got much clear idea on the topic of from this post.

  7. I am not sure the place you are getting your information, but good topic. I must spend some time learning much more or figuring out more. Thank you for magnificent info I was looking for this info for my mission.

  8. this website

    Thanks for finally writing about > عصر التافهين السياسيين:إنهم يفعلون بأوطانهم ما لا يفعله العدو بها –ستراتيجيا نيوز

  9. A book of First Class stamps trental kaina “Huawei shares the same goal as the UK government and the ISC in raising the standards of cybersecurity in the UK and ensuring that network technology benefits UK consumers,” a spokeswoman said.

  10. tivi casper

    Useful stuff. Thanks!writing argument essay dissertation examples top 10 essay writing services

  11. to read more

    I used to be suggested this blog by my cousin. I’m no longer certain whether ornot this post is written via him as no one else recognise such certain approximately my problem.You’re incredible! Thanks!

  12. Bitcoin Trading

    What’s up colleagues, how is everything, and whatyou wish for to say regarding this paragraph, in my view itsreally awesome for me.

  13. Good day! Do you use Twitter? I’d like to follow you if that would beokay. I’m absolutely enjoying your blog and look forward to new posts.

  14. kupit` tur

    Appreciate you sharing, great post.Really thank you! Want more.

  15. Iris Hutchinson

    always i used to read smaller articles or reviews that also clear their motive, and that is also happening with this paragraph which I am reading at this place.

  16. Pamela Giles

    onlinepharmacy order cipro online supreme suppliers onlinepharmacy

Leave a Reply

WC Captcha 3 + 3 =