How to access a variable from another class without making it static

Discussion in 'Programming General' started by Ordou, Nov 11, 2011.

How to access a variable from another class without making it static
  1. Unread #1 - Nov 11, 2011 at 10:42 PM
  2. Ordou
    Joined:
    Sep 11, 2011
    Posts:
    161
    Referrals:
    0
    Sythe Gold:
    0

    Ordou Active Member
    Banned

    How to access a variable from another class without making it static

    is this possible? I really need multiple instances of the objects I'm creating and when I make them static all of them share x and y coordinates and it gets annoying, does anyone know how to use variables in another class without making them static
     
  3. Unread #2 - Nov 12, 2011 at 4:02 AM
  4. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    How to access a variable from another class without making it static

    Remove the static keyword and access them as instance members of the class?

    Post some code so we can see what you're trying to do.
     
  5. Unread #3 - Nov 12, 2011 at 1:01 PM
  6. Ordou
    Joined:
    Sep 11, 2011
    Posts:
    161
    Referrals:
    0
    Sythe Gold:
    0

    Ordou Active Member
    Banned

    How to access a variable from another class without making it static

    ok I have these variable in my Object class
    Code:
    Image img;
    static Rectangle rec;
    static int x;
    static int y;
    
    then I access them in my player class for the movement(this is where it errors if the values aren't static)
    Code:
    if(up) {
    	//y-=1;
    	//rec.y-=1;
    	if(!rec.intersects(tObject.rec)) {
    	tObject.y+=1;tObject.rec.y+=1;
    	} if(rec.intersects(tObject.rec)) {	
                 tObject.y-=1;tObject.rec.y-=1;
    	}
    but when I have multiple Objects created and the x and y's are static they all share the most recent objects values like this
    Code:
    public tObject o = new tObject(500, 150, 20, 100, "images/");
    public tObject t = new tObject(500, 270, 100, 20, "images/");
    it would use the most recent tObject "t" static values instead of the two separate values for the Objects all together

    so I'm trying to make my x and y and Rectangle values non-static
     
  7. Unread #4 - Nov 12, 2011 at 2:15 PM
  8. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    How to access a variable from another class without making it static

    Make the fields non-static so their values can vary per instance.
     
  9. Unread #5 - Nov 12, 2011 at 2:46 PM
  10. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    How to access a variable from another class without making it static

    That. Static means that the variables are associated with the class, not the particular instance of the class. That means that if you make something static, there is only one value of it no matter how many objects you create from that class. If it isn't static, every time you make a new object, that object gets its own variables and they can be different from one another.
     
  11. Unread #6 - Nov 12, 2011 at 4:36 PM
  12. Ordou
    Joined:
    Sep 11, 2011
    Posts:
    161
    Referrals:
    0
    Sythe Gold:
    0

    Ordou Active Member
    Banned

    How to access a variable from another class without making it static

    Yah but it has to be static or I get an error, that's what I'm asking, is there any way around it?

    Code:
    Cannot make a static reference to the non-static field tObject.rec
    of course I could instead of tObject.rec.y use thing.e.rec.y, but then I would need to do that for every instance of tObject I created
     
  13. Unread #7 - Nov 14, 2011 at 5:34 AM
  14. wackywamba
    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1

    wackywamba Guru

    How to access a variable from another class without making it static

    You shouldn't be accessing variables like that in the first place.

    Create accessor and mutator methods (getters and setters).

    i.e. in your object class have

    Code:
    int getX(){
     return x;
    }
    
    Then in the player class you use
    Code:
    tObject.rec.getX();
    when you want to access that tObjects' x.
     
  15. Unread #8 - Nov 17, 2011 at 9:09 PM
  16. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    How to access a variable from another class without making it static

    You need to create an INSTANCE of the class. Classname variableName = new ClassName();.
     
  17. Unread #9 - Nov 18, 2011 at 8:45 AM
  18. wackywamba
    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1

    wackywamba Guru

    How to access a variable from another class without making it static

    He says that at the bottom of the post. It's an 'object' anyways not a 'class' in this context. The only way this could be a problem is if the Rectangle hasn't been initialized.

    So it's probably not that.

    It would help if you posted the error that you get when the variable is not static - as it definitely shouldn't be static.
     
  19. Unread #10 - Nov 18, 2011 at 10:41 AM
  20. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    How to access a variable from another class without making it static

    Oh. I'd like to see the whole code to figure it out then.
     
  21. Unread #11 - Nov 18, 2011 at 8:34 PM
  22. Ordou
    Joined:
    Sep 11, 2011
    Posts:
    161
    Referrals:
    0
    Sythe Gold:
    0

    Ordou Active Member
    Banned

    How to access a variable from another class without making it static

    Nevermind, I did the thing I was trying to do but realized it made every object I made act as it's own and it messed up all the coordinates so I have to do it the way I was doing it previously
     
< .NET Generate Captcha | [HTML] Website Creating Service [HTML] >

Users viewing this thread
1 guest


 
 
Adblock breaks this site