aspose file tools*
The moose likes Beginning Java and the fly likes how to forcefully execute constructors in java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to forcefully execute constructors in java ?" Watch "how to forcefully execute constructors in java ?" New topic
Author

how to forcefully execute constructors in java ?

Kanchan Ghosh
Greenhorn

Joined: Mar 26, 2007
Posts: 2
Hi,

Is there any way to execute the constructor code forcefully?

Class A {
public A() { // Constructor
System.out.println("Hello");
}
}

Suppose in a block of code we hava instantiated the A class in the following way

......... // some code
.........
A a = new A();

Now after creating the instance of A, can we explicitly execute the code(programatically) in the constructor of A. If anybody knows plzz reply.
Thanks in advance....


Thanks, <br />Kanchan
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Constructors are NOT methods, and as such can not be called!

You can do:

The calls to this this() are calls to the class constructor, much like calls to super() are calls to the super class constructor.

However, they MUST be on the first line of the constructor, and thus you can use one or the other NOT both.

Oh, and I am not saying the above code is a good or bad idea, just that it can be done.

G

[ April 11, 2007: Message edited by: Gavin Tranter ]
[ April 11, 2007: Message edited by: Gavin Tranter ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

As Gavin says, a constructor is not a method - it is a special block of code that is called to initialise a new instance of a class. So you can't call a constructor on an existing object as if it is a method.

If you need to re-initialise an existing object, you could put the initialisation code in a method, and call that method from the constructor - then later on you can call the initialisation method.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Kanchan Ghosh
Greenhorn

Joined: Mar 26, 2007
Posts: 2
Thanks Gavin and Jesper for your help......
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Gavin,

There is an error in your code, marked bold


this(x,5.5); //There is no such constructor which takes int and //double, instead make it this(x,5.5f);


I think, you make a typo, but we are SCJP guys, this can cost us dear!!!

Thanks for giving detailed example!!!

Regards,
cmbhatt
[ April 11, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Oh dear an error?
Good catch, I always forget floating point literals are actual doubles and not floats.

Not taken the SCJP exam......yet.

Good catch and thanks.

G
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to forcefully execute constructors in java ?