Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Deepakk Verma
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it should be that way:
your constructors:

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write one constructor in terms of another using "this:"



Is that what you're asking about?
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepakk Verma
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic