Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do constructors create objects? (was: is it true?)

 
Naresh Saw
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all

and sandeep, when r u going to appear for the exam?
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic