my dog learned polymorphism*
The moose likes Beginning Java and the fly likes can we invoke an overloaded constructor by object of same class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can we invoke an overloaded constructor by object of same class?" Watch "can we invoke an overloaded constructor by object of same class?" New topic
Author

can we invoke an overloaded constructor by object of same class?

Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
I was doing my method overloading book example and got this thing in mind

can we invoke an overloaded constructor by object of same class?
as explained in this code ....
i made a 3 constructors.
(1)with no parameter(assign's -1 to all the instance variables).
(2)with 1 parameter(assign's argument value to all instance variables).
(3)with 3 parameters(assign's different argument values to different instance variables).

i declared 1 object of class Box with (1)constructor..and then tried to invoke (2)constructor by that
but was not able to do it...........can any gentlemen tell me why?

Here is the code:

leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
it should be that way:
your constructors:

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You can write one constructor in terms of another using "this:"



Is that what you're asking about?


[Jess in Action][AskingGoodQuestions]
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
suggestions are nothing but same what Deepak did. in suggestions, constructor with 3 args are invoked and in Deepak code he is setting all values. Anyway Question is not very clear, what Deepak is not able to do? Code looks OK. So, tell us what you want your code to do?
Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
no.....its still not working
it should be like if i write


in the 10th line of main method....mybox1 must invoke Box(double i) constructor and should assign
20 to all the fields of mybox1....
Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
I Doubt if it is legal to do this....but i am not sure!!!

Calling constructor with a predeclared object reference variable?
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Deepakk Verma wrote:
Calling constructor with a predeclared object reference variable?


unless until reference variable is final it can be reassigned to other objects of same super type. How do you know that it is not working. Have you taken the print again to see that volume has been changed or not?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

It's totally legal to reuse a variable. In the line you just showed, though, you've left out "new", and that's not legal. If you were to run your original code, it would do exactly what you expect it to. What part do you think is not working? IN other words, what do you observe, and what did you expect to observe instead?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

Deepakk Verma wrote:I Doubt if it is legal to do this....but i am not sure!!!

Calling constructor with a predeclared object reference variable?


Yeah, it's legal. You create a new (thus the keyword new) object pointed to by the same reference. Try printing out the volume again after you make this new object.


Steve
Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
the statement with new is creating a new object with all fields as 20....rather i want the fields of the previous object to change its fields value to 20 from -1
what i mean is if i make an object(1st)

Box mybox1;
mybox1 = new Box();

and appoints its reference to the box class variable "myclass1" with fields

width = -1
height = -1
depth = -1

and now i write another statement i.e., mybox1 = Box(20);

then i dont want a new object to be generated rather i want the previous object(1st) to change its
field values to change to

width = 20
height = 20
depth = 20

but writing ,,mybox1 = new Box(20);
will create a new Box object and will appoint its reference to mybox1 variable and the previous object(1st) would be lost

i know whole of this can be done with a method but i wanna know if doin this with overloaded constructor possible or not
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

i know whole of this can be done with a method but i wanna know if doin this with overloaded constructor possible or not



I don't think this question is related to overloaded questions -- but more related to ... can you re-construct an instance that has already been constructed? and without using a method? Calling a constructor to reinitialize an instance?

Answer: No.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Deepakk Verma wrote: mybox1 = Box(20);


constructor is not same as method call. So, you should be getting compile time error invoking that code. Constructor always is used to create an object with possibly way to initialize parameters.

Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
that is exactly i am asking you guys
look its all illegal to do..................actually the whole of this question is itself illegal
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Deepakk Verma wrote:that is exactly i am asking you guys


Your doubt is clear, you mean, then why this
Deepakk Verma wrote:
look its all illegal to do..................actually the whole of this question is itself illegal


I believe this was attempt to save your face. The answer you knew, but had a shadow of doubt. When you got the same answer, what you thought should be right, you discredited everybody in this discussion. But you don't have to. having shadow of doubt is alright.
Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
I believe this was attempt to save your face. The answer you knew, but had a shadow of doubt. When you got the same answer, what you thought should be right, you discredited everybody in this discussion. But you don't have to. having shadow of doubt is alright.


i admit that i had that shadow of doubt.....but i am not discrediting anybody
only the thing is that i forgot to acknowledge all of you........i am sorry for that
and off course , thanks a lot to all you gentlemen for clearing my doubt.
Thanks again,
Deepak Verma
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Create a method "init()" which initializes the member variables of the object. Call it from the constructor, and allow other code to call it by making it public:



Then any time you want, you can say something like

mybox1.init(20);

and reset the variables in your original object.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
Rahul.p Kumar wrote: . . . attempt to save your face. . . .
Please!

That is hardly a friendly thing to say. As you say, we can all be unsure about things. The whole idea of the atmosphere of JavaRanch is that nobody gets annoyed about it.
Abhaya Patel
Greenhorn

Joined: Feb 27, 2008
Posts: 2
Hello Deepak,

What ever is your code is absolutely right and it is working. You have to do a single change. You have declared the Box class a public class and the class BoxDemo is also a public class. But it not possible to put two public class is a single file so either remove the public keyword from the class Box or shift the Box class to other file.


Cheer

Abhay
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: can we invoke an overloaded constructor by object of same class?
 
Similar Threads
Inheritance, and the mis-use of the 'static' keyword
Return Statements
static reference to nonstatic var & methods
End of file while parsing
(Inheritance) please Help!