aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why constructors" Watch "why constructors" New topic
Author

why constructors

Raghav Mathur
Ranch Hand

Joined: Jan 12, 2001
Posts: 641
why do we need to difine constructors explicitly
public class xyz
{
public xyz()
{
//some code;
}
}
can somebody explain .


Raghav.
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
When you want to create an instance of the object and make sure that some default processing has occurred,before any methods on that instance can be called, you use the connstructo and place the code there.
Sometimes you can pass parameters to the constructor so that each instance of the object has different values associated with it.
If you had a class Car with a constructor that took the model name as an argument, then you can create instances of Car object by specifying the model name of the car .
For simple objects, where you do not need any variables initialized or any processing to be completed when the instance is created, you don't need to provide any constructor, as is often the case with examples programs you see in books and tutorials.
HTH
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
raghav
Another case in which you need to define an explicit constructor is if you define any constructors in a class that take arguments.
If you do not define any constructors the compiler will create a default (no argument) consructor for you. If you create any other constructors the compiler assumes that you know what you are doing and will not create a default constructor for you.
This can lead you into trouble if you try to create an instance of that class and do not call one of the constructors you defined. Usually this occurs when trying to create an instance of a subclass and then don't include a specific call to super(args), the compiler automatically inserts a call to the superclasses default constructor.
Check this code out:

In this example at the line with the ****** the compiler will insert a call to super() - the default constructor of SuperClass. Since there is a constructor defined for SuperClass the compiler will not create a default constructor and the call to super() wont compile because the constructor doesn't exist.
hope that helps


Dave
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Dave Vick:
raghav
Another case in which you need to define an explicit constructor is if you define any constructors in a class that take arguments.
If you do not define any constructors the compiler will create a default (no argument) consructor for you. If you create any other constructors the compiler assumes that you know what you are doing and will not create a default constructor for you.
This can lead you into trouble if you try to create an instance of that class and do not call one of the constructors you defined. Usually this occurs when trying to create an instance of a subclass and then don't include a specific call to super(args), the compiler automatically inserts a call to the superclasses default constructor.
Check this code out:

In this example at the line with the ****** the compiler will insert a call to super() - the default constructor of SuperClass. Since there is a constructor defined for SuperClass the compiler will not create a default constructor and the call to super() wont compile because the constructor doesn't exist.
hope that helps

Dave,
So, in this case, we need to add a no-argument
constructor to SuperClass to make the code compile, since the compiler will generate a call
to this constructor in Sub constructor. Correct me if I am wrong.
victor
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Boy, Why constructors? :roll:
after 260 posts?.....
- satya


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Originally posted by victor gu:

So, in this case, we need to add a no-argument
constructor to SuperClass to make the code compile, since the compiler will generate a call
to this constructor in Sub constructor. Correct me if I am wrong.
victor

That is correct, Victor
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why constructors