Hi
can any one tell me what is functionality of constructor....................?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi
can any one tell me what is functionality of constructor....................?
It is code that is executed when you create an object.
As the first responder asked you, have you tried google before posting it here? I think most of us get tempted to post questions even though they might be within reach (answers). Nevertheless, I will answer that for you: A constructor is like a method but it MUST have the same name as the class and it is used to initialize the members of that class. It can have parameters or be default - without parameters. Consider:
I hope this will help you.
Last edited by helloworld922; January 31st, 2012 at 11:52 AM.
You aren't helping the OP much (regardless of how simple the question is) by posting unformatted code. Nobody wants to read it. Put your code in code blocks please.
In response:
Like qurtan stated, a constructor is code that is run when the object is initialized. You can use it to set variables the Object needs at the start of its life or make the caller provide the object with vital information it needs to function.
[CODE] public class MyObject { //Note the public keyword. Constructor access can be modified just like a method. Public, protected, private and default are all supported. //Also note that a constructor MUST have the same name as the class and compilation unit. Otherwise it is invalid. public MyObject() { //Initialization code here } //Note the difference between a constructor and a method. Methods have return types (like void -aka- return nothing). public void sayHello() { System.out.println("Hello world"); } } A constructor with parameters: public class Website { URL url; public Website(URL url) { this.url = url; } } [/CODE]
Constructors are also inherited by superclasses, and you can call upon the superclass constructor at the beginning of the subclass constructor using the super keyword.
Last edited by bgroenks96; December 29th, 2011 at 10:50 PM. Reason: Added highlights
Thanx fot this great info. I was needy about this.. Thanks you have solved my problems
Just to add some rules which will apply to constructors other then these constructor is just a normal method.
Constructor Rules
A constructor can't have a return type.
Constructor must have the same name as class name.
Constructors can't be marked static
Constructor can't be marked Final or abstract
Note: Constructor can't be overridden.
Quoted from URL