Java Constructors

in #java5 years ago

An In Depth Discussion

codes-coding-computer-270623.jpg
Image courtesy of pexels.com

My month of Java is over but I am still keeping the faith. I am still trying to continue the momentum I started back at the start of October by continuing with my learning journey of the Java programming language. In this case though I am going to try and be a little more thorough with my posts from now on and hopefully posting about once a week. In another way hope I am able to bring some of my new experience to new readers as well. In this post we are going to go a little more in depth with Java Constructors, as they were something I didn't really understand the first time I was learning Java.

So What Are Constructors and Why Do We Need Them?


If you have been worked with a constructor before you may be wondering why you need to add them into your code. A Java class will work without them so what is the point. Well this may be true for smaller classes or code but as things grow, so to may be the number of variables that are needed to initialize a new class. With that comes the fact that a programmer can easily make a mistake or fail to initialize some fields and in this case the program will then behave incorrectly. To avoid this situation, we need to restrict our program to make sure all fields are gathered and specified when they are created, otherwise the creation of the object will fail.

This is exactly where constructors come in as they create a skeleton that each new object must match. So if we have a new class to create a car:

public Car(String model, int maxSpeed) {
   this.model = model;
   this.maxSpeed = maxSpeed;
}

If we then create an instance of a class, we now need to do this.

public static void main(String[] args) {
   Car ford = new Car("Ford Focus", 180);
}

In our example above you will notice the constructor is declared in a similar way to a method, but it does not have a return type. As well the constructor will specify the class name, starting with the uppercase letter. Finally, you will notice the constructor uses the "this" keyword, to indicate we are adding details for this particular object.

As above, a constructor is basically a method but with some special features. We need to pass arguments to our constructor just like our Java methods and calling the constructor won't work unless you specify them, and just like methods, constructors can be overloaded.

As we said at the start of this article, a Java class will still work without a constructor specified and this is because the compiler will add a default constructor if you do not create one. It won't take any arguments but will be invoked every time you create an object of the class.

So to answer our original question, is we need constructors to help programmers. If you initialize fields manually, there is a risk that you might miss something and introduce a bug, but this won't happen with a constructor. If you fail to pass all the required arguments or pass the wrong type of arguments, the compiler will throw an error and this should be an easy fix. We must also separately say that you should not put your program's logic inside a constructor. This is what methods are for. Methods are where you should define all the required functionality.

If you have found this post useful or interesting, please consider Commenting, Upvoting, Following and/or Resteeming @run.vince.run

Sort:  

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

Coin Marketplace

STEEM 0.31
TRX 0.12
JST 0.034
BTC 64742.01
ETH 3172.49
USDT 1.00
SBD 4.10