• Post Reply Bookmark Topic Watch Topic
  • New Topic

call constructor  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me with sample code how to call constructor ?
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to create an instance of an object? Then you're already calling a constructor.

For more details, I'd suggest starting with the Java tutorials. This section introduces you to constructors.
 
cherry hal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have one doubt, can i call constructor from another class?
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cherry hal wrote:I have one doubt, can i call constructor from another class?

Sure. In fact it's pretty much required unless you're writing a factory method.

Winston
 
cherry hal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks winston,
can you give me a sample code for that?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cherry hal wrote:Thanks winston,
can you give me a sample code for that?
HIH

Winston
 
Greenhorn
Posts: 3
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You Cannot Call a constructor from another Class, the constructor of a class is called whenever a new instance is created..

However, you can call the constructor of a class from within the class using the "this()" keyword..

You can call the constructor of the parent class by using the "super" keyword..
 
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You Cannot Call a constructor from another Class, the constructor of a class is called whenever a new instance is created


And since you are very likely to be creating new instances in other classes, you call the constructor. Of course you can call a constructor from another class.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Ashok Tiyadi.

Please don’t say child and parent when you mean subclass and superclass. I think that exampe, unfortunately, with this(), was too complicated for cherry hal’s requirements
And welcome, too,cherry hal
 
Ashok Tiyadi
Greenhorn
Posts: 3
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK.. My Mistake .. :-)
 
Ashok Tiyadi
Greenhorn
Posts: 3
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:
You Cannot Call a constructor from another Class, the constructor of a class is called whenever a new instance is created


And since you are very likely to be creating new instances in other classes, you call the constructor. Of course you can call a constructor from another class.

But how can we call a constructor from another class.. ?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashok Tiyadi wrote:
James Boswell wrote:
You Cannot Call a constructor from another Class, the constructor of a class is called whenever a new instance is created


And since you are very likely to be creating new instances in other classes, you call the constructor. Of course you can call a constructor from another class.

But how can we call a constructor from another class.. ?


It depends what you mean by "call a constructor." You can only directly call a constructor of the current class or its superclass, using this(...) or super(...), and you can only do that as the first statement in a constructor for that class.

However, you can cause a particular constructor to be invoked simply by creating an object using the new operator and specifying the appropriate class name and arguments. I'm sure you know how to do that, and if you don't, you just need to study the appropriate sections of your Java text or tutorial.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!