Results 1 to 6 of 6
Thread: Storing an object in a variable
- 08-02-2012, 01:21 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
Storing an object in a variable
Right, I'm trying to explain my problem as clearly as I can, but it is kind of difficult to do even if the problem is really simple. The following code doesn't work, but it (hopefully) explains what I am trying to do:
This code, however, works:Java Code:myClass selection; if(/**something**/) { selection = mySubClass1; // This isn't possible } else if (/**something else**/) { selection = mySubClass2; // This isn't possible } myClass myObject1 = new selection; // This isn't possible either myClass myObject2 = new selection; // This isn't possible either class myClass { int myVariable; } class mySubClass1 extends myClass { mySubClass1() { myVariable=1; } } class mySubClass2 extends myClass { mySubClass2() { myVariable=2; } }
However, now myObject1 and myObject2 store the both the exactly same object.Java Code:myClass selection; if(/**something**/) { selection = new mySubClass1(); // Works just fine } else if (/**something else**/) { selection = new mySubClass2(); // Works just fine } myClass myObject1 = selection; // This is the problem myClass myObject2 = selection; // This is the problem class myClass { int myVariable; } class mySubClass1 extends myClass { mySubClass1() { myVariable=1; } } class mySubClass2 extends myClass { mySubClass2() { myVariable=2; } }Last edited by Derpy; 08-02-2012 at 02:06 PM.
- 08-02-2012, 01:38 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Storing an object in a variable
You need some real code, because those 'new' statements are invalid anyway.
Because you are having problems with something it's hard for us to tell what those problems are (ie syntactic?) without some actual code that represents your problem.
In a forum situation like this where posters have wildly varying levels of ability, paraphrasing can lead to misunderstandings.
So...what are you trying to achieve?Please do not ask for code as refusal often offends.
- 08-02-2012, 02:17 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
Re: Storing an object in a variable
Right, it is kind of hard to explain. Basically, my goal is to create a program where I can draw particles with my mouse. I am trying to create a system where I can use my keyboard to select different particle types. Each particle type is a class than extends this main particle class. Then I am somehows supposed to store the class of the selected particle type inside a variable. Then I should add new particle objects to the game based on that variable.
The problem is so simple but so darn difficult to explain.
- 08-02-2012, 02:36 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Storing an object in a variable
So to take your first post:
What are you trying to achieve with those 3 object references?Java Code:myClass selection; if(/**something**/) { selection = new mySubClass1(); // This isn't possible } else if (/**something else**/) { selection = new mySubClass2(); // This isn't possible } myClass myObject1 = new selection(); // This isn't possible either myClass myObject2 = new selection(); // This isn't possible either
Obviously that code is invalid (you can't 'new' using an object, you use a constructor as you do in the later bit of code).
When you say "store the class inside a variable" do you mean you want to create an object of that type?
You know how to do that as you do it in the code above.
So that leaves "add new particle objects to the game based on that variable".
Does your game have a List of particles? If so then add this new particle to that list.Please do not ask for code as refusal often offends.
- 08-02-2012, 02:39 PM #5
Member
- Join Date
- Jul 2012
- Location
- Earth
- Posts
- 75
- Rep Power
- 0
Re: Storing an object in a variable
Sounds to me like you need to use the factory pattern. For each type of particle you would define a class the acted as a factory to create the particle. You would then select a factory and store that. See Factory method pattern - Wikipedia, the free encyclopedia but Google is your real friend.
- 08-02-2012, 03:13 PM #6
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Storing object with int values in Arraylist.
By Zigster in forum New To JavaReplies: 12Last Post: 05-26-2012, 09:49 PM -
Storing System.out to a variable
By JEltgroth in forum New To JavaReplies: 5Last Post: 12-29-2010, 02:03 PM -
Problem in storing Socket class object in ArrayList array
By Anoop in forum New To JavaReplies: 2Last Post: 10-28-2010, 02:33 PM -
Advantages and disadvantages of storing java object to database
By archanab in forum Advanced JavaReplies: 10Last Post: 12-23-2008, 07:37 PM -
[SOLVED] Why import isnt needed whn ref is used without storing to local variable
By N_i_X in forum Advanced JavaReplies: 2Last Post: 03-31-2008, 05:11 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks