Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method and Constructor

 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everbody,
What is the difference between Constructor and method? i.e.
when i do use constructor and when i do use method of the
same class?

Can anybody clear it out.
Thanks,
Sayeed.
 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply put, a constructor is a method that is called when an object of your class is created.
------------------
- Daniel
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,
Thanks for your reply.
You are right that Constructor is a method. Actually my question was why and when i need to create Constructor and method in the
same class. I think in the mean time i learnt from other source
about Constructor and method. I am trying to give as below what
i learnt.
For instance suppose you think a package named java.awt.*;
In this package you would get a class named 'Dimension' i.e.
import java.awt.Dimension;
Ok. In this Dimension class you would get three things,
1. Hieght or Width -<Field>Member variables.
2. Dimension(int height, in width) -<Constructor> Same as Class
name.
3. getHeight(),getWidth() -<Method>But with different
name in the same class.
Now if you want to create a Dimension of any object like image object ok, then you have to create an instance i.e. object of class in this way,
new Dimension(40,40);
And if you want to get the height and width of that image
, then you have to use the method in this way,

new Dimension(image.getWidth(this), image.getHeight(this));
So Conclusion is that if you make an object i.e. instance of
class you need Constructor and if also you want to change
or modify or whatever you use to that Constructors, you need METHODs.
Here Dimension Constructor has changed image's height and
width using METHOD getwidth() and getheight().
So Constructor makes an object and Methods need to do modifying the attributes of that object.
Is it correct? Kindly clarify that.
Thank, Sayeed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic