0

Can anybody please tell me what the Constructors are used for exactly in Java?

What could be the ideal condition for using the constructors in an application?

Thanks, david

5
  • 2
    A very generic question, Googling would have sufficed. Commented Nov 12, 2010 at 6:17
  • 1
    +1 as valid question, as some time u want more focus answer by putting that on Stack Overflow rather then goggling . Commented Nov 12, 2010 at 6:32
  • 1
    @Hansmukh that's right otherwise what is the need of Stackover flow as We have Google already to serve us almost everything we want from it eventualy. Commented Nov 12, 2010 at 6:51
  • 1
    i agree with you. you will get more professional views comments in less time here . Commented Nov 12, 2010 at 6:57
  • I meant it for this question specifically, not generally. Anyway, doesn't matter as such. Commented Nov 12, 2010 at 8:09

8 Answers 8

3

Constructors are used to assign the values to the properties attached with the instance of the class.So before using any object of class it will be available with required values and ready to use.

Hope this will help you.

Sign up to request clarification or add additional context in comments.

Comments

2

Constructors are very useful for initializing instance variables. For instance, say you have a User class, and you would like to initialize the userName property whenever you create a new instance:

Class User 
{
    String userName;      

    public User (String name)
    {
        userName = name;
    }
}

User oUser = new User("Mike");

Note - When you write your own constructor, java doesn't provide a default constructor

2 Comments

Note that in Java, class names (like String) are capitalized, and vars (like userName) begin with a lowercase letter.
@akf, thank for mentioning that , actually i just wana give an idea how that works
1

Creating an instance of an object. You use constructors any time you use object-oriented design in an application.

Comments

1

They are used to initialize objects. You should take a look at some good OO tutorial. Here's one: http://www.javaworld.com/javaworld/jw-04-2001/jw-0406-java101.html. Specifically, this page: http://www.javaworld.com/javaworld/jw-04-2001/jw-0406-java101.html

Comments

1

Constructors are a very basic aspect of any object oriented programming language. I suggest you do some googling on object oriented programming and constructors and then come back and ask questions if you are still unsure.

Comments

1

Constructors are used to initialize the class fields when the new object of the class is made.

Moreover in the singleton design pattern you will see the more uses of the constructor where the single instance of the class is made in the constructor being the constructor private, so that no other object of the class can be made.

Comments

0

A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes.

They have no return type, not even void. This is because the implicit return type of a class constructor is the class type itself.

Class ConstructorExample{
    String agentJamesBond;      

    ConstructorExample(String number)
    {
        agentJamesBond= number;
    }
    //various methods(member of class ConstructorExample) which uses instance variable agentJamesBond.
}

//at the point of creation of the constructor

ConstructorExample xyz= new ConstructorExample("007");

They are created by default, if not defined explicitly.

It's job is to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately.

Comments

-1

A constructor is creating an instance of a class. Note, that what's created is the object, not an instance of an object. There might be some initialization in a constructor, but the main functionality of all the constructors is: creating the object.

5 Comments

'Instance of an object' is meaningless, so introducing it is pointless. Constructors both construct and initialize. Distinctions without a difference.
Dear EJP, before you're downvoting anybody, please read the comments carefully. I was not saying that it's an instance of an object, I said it's not an instance of an object, correcting @Rafe Kettler's comment. Before you're downvoting anybody, please read the comment and don't downvote for something which is NOT THERE.
And dear EJP, you didn't see a constructor where initialization didn't happen?
Actually, the constructor doesn't "create" the object. The new opcode creates the object, the constructor initializes it. When you say new Foo(), the resulting bytecode performs these two steps in sequence. See java.sun.com/docs/books/jvms/second_edition/html/…
if you are commenting on someone else's answer, the place to do it is in a comment under that answer. I don't know what your other question means. The object always has to be initialized and that's what constructors do.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.