• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why do we use a contsructor in a class ?

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

Can anybody tell me all reasons to use a constructor in a Class !

rather "Why do we use a constructor in a class ?"
[ June 24, 2008: Message edited by: sachin verma ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You use a constructor to initialize the state of a newly-created object of the class.
 
sachin verma
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick reply

But Is it the only reason?
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you use a constructor mostly so you can access that classes methods and attributes parallel to other objects created (as that object instances own).

Other wise to access the methods of a class, you would need to use static methods, or abstract and inherit from that class.

Justin Fox
 
sachin verma
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham
You use a constructor to initialize the state of a newly-created object of the class



Yes I do agree with you.
Can we replace the working of the constructor with "instance block" !\\Line one

I do not agree with the "Line one" in this post because Instance block run right after all the super class's constructor but before the class's constructor (in which there is that instance block).
Instance block can not stand alone itself without the mechanism of constructors.So it can't replace the working of constructors.

Am I Right?

Originally posted by Justin Fox
you use a constructor mostly so you can access that classes methods and attributes parallel to other objects created (as that object instances own).


If it is true, I think it is very deep aspect !
Can you please explore it more?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a constructor that contains parameters, usually the constructor initializes the instance variables with those parameters.

If you have a constructor that contains parameters and you want to subclass the class in question, you either need to 1) create matching constructor(s) in the subclass or 2) create an additional no arg constructor.

If you don't have a constructor that contains parameters, you do not need to specify a constructor. Java will create an invisible "default no arg constructor" for you.

When you create a new instance of your class, at least one constructor will be used.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I say that we use contructor in a class to force somesome to obey the rule of creating the instance of the class.

is someone disagree with me..?
[ June 25, 2008: Message edited by: Angel Rajiv ]
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think of your constructor as a cookie cutter. You can make many cookies in the shape and size with the cookie cutter.
Tis such a pity I can't call setSize(GIGANTIC) on my cookies.
reply
    Bookmark Topic Watch Topic
  • New Topic