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 Is it possible to construct the same object twice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is it possible to construct the same object twice?" Watch "Is it possible to construct the same object twice?" New topic
Author

Is it possible to construct the same object twice?

Sergio Campos J.
Ranch Hand

Joined: Jun 01, 2009
Posts: 53
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?

"There are times when the easy things should be quick and easy" [...] "and worry about the theory later" -Fred Hamilton-
Bhagat Singh Rawat
Ranch Hand

Joined: Apr 04, 2009
Posts: 93
Sergio CamposJ wrote:Considere these two lines:




is this java code?


Brainbench Java 2.0 Fundamentals, J2EE 1.4
http://www.brainbench.com/transcript.jsp?pid=8192792
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 06, 2000
Posts: 177
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Steve Luke wrote: most of us aren't mind readers.

I knew you were going to say that.
Sergio Campos J.
Ranch Hand

Joined: Jun 01, 2009
Posts: 53
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

Joined: Aug 16, 2005
Posts: 14266
    
  21

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Sergio Campos J.
Ranch Hand

Joined: Jun 01, 2009
Posts: 53
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

Joined: Jan 10, 2002
Posts: 61413
    
  67

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

Joined: Jun 01, 2009
Posts: 53
Does it apply to primitive types?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

What happened when you tried it?
Sergio Campos J.
Ranch Hand

Joined: Jun 01, 2009
Posts: 53
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to construct the same object twice?