File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes What is the use for a Constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the use for a Constructor?" Watch "What is the use for a Constructor?" New topic

What is the use for a Constructor?

jo buger

Joined: Feb 16, 2013
Posts: 2
I been trying to learn java for a little of a year now. However, I still do not understand what a Constructor does! I been on -COUNTLESS- tutorial sites and other forums, but I can't understand the answer they provide.

all I know of constructor is that -they are use to make a object? <-- I don't even understand that part.

I've writing constructors before, but most of the time I leave them blank, because I have no idea what it is for, and most of the time I can get away with it. It's probably not a good idea so that why I'm desperately trying to fully understand the roll of the constructors in Java programs.

CAN someone EXPLAIN to me what exactly is a constructor, and what is the use for it? explain it in such a simple manner that a 6 year old can understand it, I really need to understand this subject before going forward with my programming career. Please provide some simple codes to demonstrate the usage if you can.
K. Tsang

Joined: Sep 13, 2007
Posts: 3132

First do you know or understand what an Object is?

Object in java terms is the top level of every class. For example, when you write "public class Bike { }" ... the Bike class is implicitly extending Object. This means "a bike IS-A object". This "IS-A" term represents inheritance. But more on that when you get there.

So in java, everything is an object. An object can have attributes (the instance variables) that describe the object.

The constructor like what you've read "constructs" or "create" a new instance of your object or class. Every class or the JVM creates a empty constructor if you don't put one in. Using the Bike class, the following 2 pieces of code is the same from a JVM point of view.

The java tutorial section Object Oriented Programming Concepts here talks about all this stuff.

You may also want to read Head First Java.

Hope this helps.

K. Tsang JavaRanch SCJP5 SCJD OCPJP7 OCPWCD5 OCPBCD5 OCPWSD5 OCMJEA5 part 1 part 2/3
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi Jo.

My answer to your question is the same that any book would tell you. A constructor is simply a member of a class which has the same name as the class, no return type and can have 0 or more arguments. It is used to instantiate or create objects of that particular class and then assign values to instance variables for that object.

I would suggest that you read Kathy Sierra - Bert Bates' book SCJP for Java 6.(specifically the chapter on OOPS and constructors) It is written in lay-man language and is very easy to understand. I was in a similar position when I started with my career. I was confused about most of things/concepts. Confidence comes with practice and consistency. Also, make sure that you use notepad for writing and running Java while you are in initial phases. Do not use an IDE such as Eclipse or NetBeans directly. The reason is that IDEs correct many things for you so you will never be able to fully understand why things are happening the way they do and what is going on behind the scenes.(like how we set classpath etc..) So type code in an editor such as notepad, compile it via the command prompt and then run it. Get your hands dirty. It will help you in the long run.

~ Mansukh
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 8942

jo buger wrote:all I know of constructor is that -they are use to make a object? <-- I don't even understand that part.

After a year? I hate to say, but if that's the case then maybe programming (or possibly just Java) isn't for you, because you can't have done anything very meaningful without them. And it's not a criticism; I know a lot of very smart people who simply aren't "wired" for programming - my brother, for one (and he'd tell you the same thing).

And I'm afraid that trying to explain it to a 6-year old won't work (unless that 6 year old is very smart), because constructors are quite involved.

However, here's my attempt to make it, as Einstein said, "as simple as possible, but no simpler":

1. A constructor is a piece of code used to make an object, which looks like a method (as described by Mansukhdeep), but is called using the new operator. The code inside a constructor usually initializes fields that belong to the object.

2. Since every object is part of a hierarchy (as K.Tsang said, ALL classes descend from Object), there needs to be a way for the JVM to call all constructors in the object's hierarchy, and it's this:
  • You can add a specific call to a superclass constructor. If you do this, it must be the first statement in the constructor. This is so that the superclass's constructor is always called before anything else in your constructor is done.
  • If you don't add a call of your own, the compiler will add one for you, and it will be specifically: super() - which calls the "no args" (ie, "no parameters") constructor of the superclass (and Object already has one of these).

  • This mechanism ensures that every class in an object's hierarchy is visited with a constructor call, and that those calls occur in the correct sequence.



    Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
    Articles by Winston can be found here
    I agree. Here's the link:
    subject: What is the use for a Constructor?
    It's not a secret anymore!