Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to construct the same object twice?

 
Sergio Campos J.
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considere these two lines:

I've thinking of using something like that (in differents moments of the code)
to reset newObject to former values set by constructor in ObjectX class.

Will it work as I think?
 
Bhagat Singh Rawat
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergio CamposJ wrote:Considere these two lines:




is this java code?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergio CamposJ wrote:Will it work as I think?

It will not even compile. You'll need to be a little clearer as to what you are asking.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if it *was* legal Java you can't declare the same variable twice. You can assign a value to it twice, but not declare it.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer your question, there is no built in way to re-initialize an object to its original state. The easiest way would be to create a new instance and throw away (no longer reference) the original object.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, after you make the code compilable, and take into account that you can't declare the variable twice (but can assign to it twice if you want), you should be clear about exactly what you think it will do. After all, to answer the question "Will it work as I think?" we have to know what you think, and most of us aren't mind readers.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote: most of us aren't mind readers.

I knew you were going to say that.
 
Sergio Campos J.
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:
Steve Luke wrote: most of us aren't mind readers.

I knew you were going to say that.

You read his mind. Didn't you?
No doubt. Some of you are truly mind readers.
---o---
Doubts cleared. Thanks to all of you. Especially to Mr. Rahder and Mr. Newton.
New Instance/Throw Away (useful knowledge)
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15280
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The syntax for declaring a variable named newObject of type ObjectX and initializing it with a new instance of ObjectX is as follows:

Don't put the ( ) around ObjectX on the right-hand side of the =.

You cannot declare the same variable twice in the same scope, but you can reassign a variable later:
 
Sergio Campos J.
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ObjectX newObject = new (ObjectX);

You don't know how much I laughed when I realized what my error was.
I understood there was an error. But I thought it was the word "ObjectX"
in front of the identifier, since I still not sure about when to put/omit it.

What's the meaning of the class name in front of the identifier?
since when we say "= new ObjectX()" we are saying what class it is.

What's the use/effect of putting/omitting the class name in front of the identifier?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It declares the type of the variable and creates it. Without it, it is assumed that the variable already exists.
 
Sergio Campos J.
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it apply to primitive types?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Sergio Campos J.
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:What happened when you tried it?

Point taken. I'm spending an interesting time in reading
"How To Ask Questions On Java Ranch"
Greetings! and thanks for helping!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic