File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructors" Watch "constructors" New topic
Author

constructors

karthika gkumar
Greenhorn

Joined: Apr 15, 2008
Posts: 6
the only way a constructor can be invoked is from within another construtor.
that means we cant call a constructor just like methods right?

and

how the constructor of Object is invoked?
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
A constructor can be called by any method, since they have visibility of the constructor (The constructor can be private).
There are some rules regarding the use of super(...) and this(...)
Take a look at this and this

To create an Object, you do ths:
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

R Lopes: A constructor can be called by any method, since they have visibility of the constructor (The constructor can be private).




Will this work?


Well, a constructor is a special method whose wholesole objective is to instantiate a class. The way we use Constructors is with the help of new keyword.

A constructor can call any method in the class by using the this refrence. Well, but not to mention it is inherently unsafe to do so and should be avoided or the world may fall

The first line of a constructor is either the call to super() or another constructor using this. So, when a class ha no parent then the Object class's constructor is called.

Hope that helps.
[ April 18, 2008: Message edited by: Anubhav Anand ]
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
Why not invoke instance methods in a constructor? Suppose you have some specialized sub class of JDialog in a Swing GUI, and you (a) want to allow it to be constructed with both a JFrame and a JDialog as its parent (two different constructors needed), and (b) want to do the layout in the constructor (fields, listeners, etc.). Now you can copy the initialization code from one constructor to the other one, or just put it in an init() method, which you call from both constructors. What's wrong with that?
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Yes, I never said that you can't but, there are many scenarios where the things may become very unsafe. The thing it has to care of is a NO leak situation. Suppose a constructor calls a instance method which may register an "instance" which may not be completely instantiated at this point of time and say anothert thread starts and then it passes control to another.... and the hell braks loose.

Until, otherwise it's fine.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
OK, see what you mean. You have to be careful with what you're doing. But in some cases, it's the best way of avoiding duplicate code anyway. In your example of registration to some external source, why not call a respective method at the end of the constructor?
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Well, I am not refuting on any point that you shouldn't call a method from a constructor. What I want to emphasize is you got be careful. The condition should not be a leak condition.

Indeed there are times when it is good enough to do that but, not always.

Well, the example I quoted says, Suppose you call a method and that method reserves the refrence of an external object that is not yet initialised then it would be a very bad situation.

Again, it all depends.
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
Originally posted by Anubhav Anand:




Will this work?

It won't, because it's not the correct way to call a constructor.
You CAN call a constructor from any method, but to call a constructor you need the new keyword.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Constructors are NOT methods, and as such can not be called like methods, in any way shape or form.

constructors are for the creation of objects of the class the constructor belongs to, that is their sole function.

One constructor can call another constructor in the same class, something like this

abhishek pendkay
Ranch Hand

Joined: Jan 01, 2007
Posts: 184
originally quoted by Anubhav Anand
Well, the example I quoted says, Suppose you call a method and that method reserves the refrence of an external object that is not yet initialised then it would be a very bad situation.

how does calling that particular method from a constructor make this situation any more difficult..?? It is anyway a error prone situation if the external object is not yet initialized
[ April 21, 2008: Message edited by: abhishek pendkay ]

The significant problems we face cannot be solved by the same level of thinking which created them – Einstein
SCJP 1.5, SCWCD, SCBCD in the making
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructors
 
Similar Threads
super()
Static Constructors???
about constructor
Which statement is true for default Constructor?
constructor