| Author |
Do constructors create objects? (was: is it true?)
|
Naresh Saw
Ranch Hand
Joined: Sep 03, 2005
Posts: 66
|
|
hi is it true that if constructor of a class is called, an object will be created of the class? help me please? [ September 16, 2005: Message edited by: Barry Gaunt ]
|
naresh<br />SCJP 1.4(86%), SCWCD 1.4(78%)
|
 |
Steve Morrow
Ranch Hand
Joined: May 22, 2003
Posts: 657
|
|
is it true that if constructor of a class is called, an object will be created of the clas?
Yes. [ September 16, 2005: Message edited by: Steve Morrow ]
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Please use a meaningful topic title in future. Yes, but not always: If there is not enough memory then you could get an OutOfMemoryError and no object would be created. Same if RunTimeException is thrown, or a declared checked Exception is thrown, or System.exit(int) is called, or.... [ September 16, 2005: Message edited by: Barry Gaunt ]
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Sandeep Chhabra
Ranch Hand
Joined: Aug 28, 2005
Posts: 340
|
|
hi i think there is only one way to call the constructor of a class and that is to create an object. i mean calling : also there is no other way of creating objects. Sandy
|
Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Originally posted by Sandeep Chhabra: hi i think there is only one way to call the constructor of a class and that is to create an object. i mean calling : also there is no other way of creating objects. Sandy
Not true. There are ways using the Reflection API, but that is beyond the scope of SCJP.
|
 |
Naresh Saw
Ranch Hand
Joined: Sep 03, 2005
Posts: 66
|
|
hi friends if it is true, then what happends to Abstract classes. can we create an object of Abstract classes? If yes how? Because when we extends an Abstract class, its constructor is called that means an object must be created which is present in the memory. i m in confusion. plz help me.
|
 |
Sandeep Chhabra
Ranch Hand
Joined: Aug 28, 2005
Posts: 340
|
|
Originally posted by Barry Gaunt: Not true. There are ways using the Reflection API, but that is beyond the scope of SCJP.
Hello sir, As you said that Reflection API is beyond the scope of SCJP, thats why i thought there is only one way to create an object which is related to the exam i m going to give. Anyways thanx for updating my knowledge. Sandy
|
 |
Sandeep Chhabra
Ranch Hand
Joined: Aug 28, 2005
Posts: 340
|
|
No, we cannot create objects of abstract class. According to me the constructor of abstract class is called by the constructor of its derieved class. and this is just to initialize the instance variables or whatever other things are required, before creation of an object. coz if derieved class would not call the constructor of super class and still use the instance variable in uninitialized state, that would create problems. Sandy
|
 |
Naresh Saw
Ranch Hand
Joined: Sep 03, 2005
Posts: 66
|
|
hi sandy thanks but thats why i asked : " is it true that if constructor of a class is called, an object will be created of the class? " does it apply to abstract class or in inheritance. e.g. abstract class Abs{ Abs(){ System.out.println("Abs"); } } class A extends Abs{ A(){ System.out.println("A"); } class B extends A{ B(){ System.out.println("B"); } } class C extends B { C(){ System.out.println("C"); } public static void main(String arg[]){ C c = new C(); //1 At line 1, how many objects will be created? thanks naresh
|
 |
Sandeep Chhabra
Ranch Hand
Joined: Aug 28, 2005
Posts: 340
|
|
As per my knowledge, only one object is created and that of type C. the construstors up the hierarchy are called so that if there is some initilization code, or some other code that should be executed before an object is creted, is present then it is excuted. The main purpose would obviously be initialization of variables of classes Abs, A,B. Hope this help you. Sandy
|
 |
Naresh Saw
Ranch Hand
Joined: Sep 03, 2005
Posts: 66
|
|
thanks but according to my knowledge calling constructor doesn't guarantee the object will be created. If constructor is called with new operator then object will be created. Plz correct me If I m wrong. bye
|
 |
Sandeep Chhabra
Ranch Hand
Joined: Aug 28, 2005
Posts: 340
|
|
Originally posted by Naresh Saw: thanks but according to my knowledge calling constructor doesn't guarantee the object will be created. If constructor is called with new operator then object will be created. Plz correct me If I m wrong. bye
"but according to my knowledge calling constructor doesn't guarantee ..." Naresh what other way do you know of calling a constructor? Sandy
|
 |
Naresh Saw
Ranch Hand
Joined: Sep 03, 2005
Posts: 66
|
|
implicit call in inheritance hierarchy. as given in my example class A{ } class B extends A{} in this case constructor of A is called implicity if u call the constructor of B. isn't it?
|
 |
Sandeep Chhabra
Ranch Hand
Joined: Aug 28, 2005
Posts: 340
|
|
Naresh this is what i meant, that YOU cannot call constructor by any other means. ofcourse the constructor implictly calls the super class constructor. BUT YOU DONT. YOU can only call constructor with new keyword and thus object is always created. Exceptions are there as stated by Mr. Barry. Sandy
|
 |
Naresh Saw
Ranch Hand
Joined: Sep 03, 2005
Posts: 66
|
|
thanks all and sandeep, when r u going to appear for the exam?
|
 |
Sandeep Chhabra
Ranch Hand
Joined: Aug 28, 2005
Posts: 340
|
|
Hi Naresh, Most probably i would be appearing for the exam this month. (I hope so...) Sandy [ September 16, 2005: Message edited by: Sandeep Chhabra ]
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Originally posted by Sandeep Chhabra: Naresh this is what i meant, that YOU cannot call constructor by any other means. ofcourse the constructor implictly calls the super class constructor. BUT YOU DONT. YOU can only call constructor with new keyword and thus object is always created. Exceptions are there as stated by Mr. Barry. Sandy
You can explictly call a constructor by using super(<actual parameters>) or this(<actual parameters>) . These calls, if made, must be used as the first statement of other constructors. Their purpose is to delegate part of the initialization process to other constructors that may already have been provided. These calls to constructors do not return any object, they are used to initialize the state of the same object currently being constructed. [ September 17, 2005: Message edited by: Barry Gaunt ]
|
 |
 |
|
|
subject: Do constructors create objects? (was: is it true?)
|
|
|