aspose file tools*
The moose likes Beginning Java and the fly likes new Object from method 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 "new Object from method" Watch "new Object from method" New topic
Author

new Object from method

Ryan Ramsdenr
Greenhorn

Joined: Dec 03, 2011
Posts: 10



ERROR: "(" or "{" expected
What can I do make a new object from the method arguments?

If someone could help me out that would be Amazing!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

why new i? use older one... or new <classname>
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Welcome to the Ranch, Ryan!

Look at any example of using the new operator, such as this one: http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

Now look at what you're doing.

See the difference?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Seetharaman Venkatasamy wrote:why new i? use older one...


Yeah, there's that too.

Ryan, it's not really clear what you're trying to do, so it's not obvious what the correct advice to give you is. However, in terms of the particular syntax error you're seeing, you need to review how to instantiate objects, as I suggested.
Ryan Ramsdenr
Greenhorn

Joined: Dec 03, 2011
Posts: 10
Jeff Verdegan wrote:
Seetharaman Venkatasamy wrote:why new i? use older one...


Yeah, there's that too.

Ryan, it's not really clear what you're trying to do, so it's not obvious what the correct advice to give you is. However, in terms of the particular syntax error you're seeing, you need to review how to instantiate objects, as I suggested.



Im trying to create a new object of whatever I put as an argument in that method.


The variable "i" Will always change, So I can't really go back.

I basicly want to do

Object x = new i() <-- A NEW Object from the method arguments
getWorld().addObject(x, GuiItem.px, GuiItem.py);

Sorry for my bad explanation, but do you understand yet?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

I assume you want to create a new object equal to i . then get all the values from i and create fresh copy.. is that you want?
Ryan Ramsdenr
Greenhorn

Joined: Dec 03, 2011
Posts: 10
Jeff Verdegan wrote:
Seetharaman Venkatasamy wrote:why new i? use older one...


Yeah, there's that too.

Ryan, it's not really clear what you're trying to do, so it's not obvious what the correct advice to give you is. However, in terms of the particular syntax error you're seeing, you need to review how to instantiate objects, as I suggested.


Okay.

I want to add a NEW Instance of i, Not just i, A New i.. Just It won't let me do that.!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ryan Ramsdenr wrote:
Jeff Verdegan wrote:
Seetharaman Venkatasamy wrote:why new i? use older one...


Yeah, there's that too.

Ryan, it's not really clear what you're trying to do, so it's not obvious what the correct advice to give you is. However, in terms of the particular syntax error you're seeing, you need to review how to instantiate objects, as I suggested.


Okay.

I want to add a NEW Instance of i, Not just i, A New i.. Just It won't let me do that.!


i is a variable, so you can't create a "new instance of i". You can only create an instance of a class. So, if you're saying that you want to create a copy of the object referred to by variable i, then the most common way to do that is with a "copy constructor." In your Interactive class, create a constructor that takes another Interactive as an argument, and then copies the fields from that one to the new one. Then you can call new Interactive(i).

Is that what you're talking about?

EDIT: And if you want that to work for other classes besides just Interactive, then you'll need another approach. Using the Cloneable interface and clone() method is probably the easiest way, but it depends on your specific requirements.
 
wood burning stoves
 
subject: new Object from method