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.
You down with OOP? Yeah you know me!
Joined: Mar 25, 2008
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?
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.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt