Results 1 to 6 of 6
  1. #1
    lifedistroy is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Help with instances of classes and variables!

    Okay, so i am making a fairly complicated game (in my opinion it is atleast) and right now im trying to figure out how to grab variables from one instance to another instance.



    I made a picture to help people understand what i want.

    Is it possible to send a variable from one instance to another instance? or maybe through the parent class.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Help with instances of classes and variables!

    Via constructor? Or if the object is already created via setter?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Help with instances of classes and variables!

    Don't send variables, call methods. (I'm not really sure what "sending variables" might be, but it doesn't sound good.)

    Of course the second instance - the one on the right - will have to have a reference to the first in order to call its methods. But in this regard it is no different to any other caller of the first instance's methods. The reference could be passed in a constructor or setter as eRaaaa suggests. Or the method call could be mediated by some other object. It's hard to be specific about how you might do it, without knowing what it is you are trying to do.

  4. #4
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    39
    Rep Power
    0

    Default Re: Help with instances of classes and variables!

    I'm not 100% sure on this, but couldn't you just declare the variable static in the parent class? That way you should modify the same variable no matter what class instance the variable is modified by (or at least that's my hopeful interpretation :( )

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with instances of classes and variables!

    Quote Originally Posted by chaucer345 View Post
    I'm not 100% sure on this, but couldn't you just declare the variable static in the parent class? That way you should modify the same variable no matter what class instance the variable is modified by (or at least that's my hopeful interpretation :( )
    No, I strongly disagree with this recommendation as you would be throwing the benefits of object-oriented programming to the wind. Do not make anything static without a good and definite reason to do so, and this isn't it. Much better would be to go with eRaaaa and pbrockway's recommendations.

  6. #6
    lifedistroy is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Help with instances of classes and variables!

    Java Code:
        public void buildReleasedAction(){
    		if(buildtwo==true){
    			if(isWall!=true){
    				//increase number of platforms
    				int temp=(Integer) enviro.tempPlatforms.get(0);
    				temp++;
    				enviro.tempPlatforms.set(0, temp);
    			}else{
    				//increase number of platforms
    				int temp=(Integer) enviro.tempWalls.get(0);
    				temp++;
    				enviro.tempWalls.set(0, temp);
    				temp=(Integer) enviro.tempPlatforms.get(0);
    				temp++;
    				enviro.tempPlatforms.set(0, temp);
    			}
    			
    			if(isWall!=true){
    				if(initialX>endX){
    					int temp;
    					temp=initialX;
    					initialX=endX;
    					endX=temp;
    				}
    				enviro.tempPlatforms.add(initialX);
    				enviro.tempPlatforms.add(endX);
    				enviro.tempPlatforms.add(initialY);
    			}else{
    				if(initialY>endY){
    					int temp;
    					temp=initialY;
    					initialY=endY;
    					endY=temp;
    				}
    				enviro.tempPlatforms.add(initialX);
    				enviro.tempPlatforms.add(initialX+20);
    				enviro.tempPlatforms.add(initialY);
    				enviro.tempPlatforms.add(0);
    				System.out.println("added a platform ontop of wall");
    				enviro.tempWalls.add(initialY);
    				enviro.tempWalls.add(endY);
    				enviro.tempWalls.add(initialX);
    			}
    			
    			if(isWall==true){
    				if(isRoad==true){
    					enviro.tempWalls.add(0);
    				}else if(isRoad==false){
    					enviro.tempWalls.add(1);
    				}
    			}else if(isRoad==true){
    				enviro.tempPlatforms.add(0);
    			}else if(isRoad==false){	
    				enviro.tempPlatforms.add(1);
    			}
    		}
        }
    so this code is in my parent class and as you can see it uses a lot of variables directly from a whole bunch of different instances of different classes. I want to put this in another instance of a class and somehow make it so that i dont have to use the parent class as a mediator between the variables of different instances. I hope this is possible or else ill just keep it like this.

Similar Threads

  1. Replies: 2
    Last Post: 01-23-2012, 12:50 AM
  2. Replies: 6
    Last Post: 10-02-2011, 06:55 PM
  3. Passing variables through classes?
    By EternalFacepalm in forum New To Java
    Replies: 4
    Last Post: 04-27-2011, 01:46 AM
  4. Replies: 4
    Last Post: 02-07-2011, 08:38 PM
  5. Deleting instances of classes from a list
    By Gmurph03 in forum New To Java
    Replies: 8
    Last Post: 11-27-2009, 12:41 PM

Posting Permissions

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