Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By sabre150

Thread: Storing an object in a variable

  1. #1
    Derpy is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default 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:

    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;	
         } 
    }
    This code, however, works:

    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;	
         } 
    }
    However, now myObject1 and myObject2 store the both the exactly same object.
    Last edited by Derpy; 08-02-2012 at 02:06 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default 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.

    ** This space for rent **

  3. #3
    Derpy is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default 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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Storing an object in a variable

    So to take your first post:

    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
    What are you trying to achieve with those 3 object references?
    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.

    ** This space for rent **

  5. #5
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Storing an object in a variable

    Quote Originally Posted by Derpy View Post
    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.
    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.
    Derpy likes this.

  6. #6
    Derpy is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: Storing an object in a variable

    Quote Originally Posted by sabre150 View Post
    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.
    This was a very helpful piece of information, thanks!

Similar Threads

  1. Storing object with int values in Arraylist.
    By Zigster in forum New To Java
    Replies: 12
    Last Post: 05-26-2012, 09:49 PM
  2. Storing System.out to a variable
    By JEltgroth in forum New To Java
    Replies: 5
    Last Post: 12-29-2010, 02:03 PM
  3. Replies: 2
    Last Post: 10-28-2010, 02:33 PM
  4. Replies: 10
    Last Post: 12-23-2008, 07:37 PM
  5. Replies: 2
    Last Post: 03-31-2008, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •