Adblock breaks this site

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

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

  1. Ordou

    Ordou Active Member
    Banned

    Joined:
    Sep 11, 2011
    Posts:
    161
    Referrals:
    0
    Sythe Gold:
    0
    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
     
  2. Jimmy

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25
    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.
     
  3. Ordou

    Ordou Active Member
    Banned

    Joined:
    Sep 11, 2011
    Posts:
    161
    Referrals:
    0
    Sythe Gold:
    0
    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
     
  4. Jimmy

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25
    How to access a variable from another class without making it static

    Make the fields non-static so their values can vary per instance.
     
  5. SuF

    SuF Legend
    Pirate Retired Global Moderator

    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
    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.
     
  6. Ordou

    Ordou Active Member
    Banned

    Joined:
    Sep 11, 2011
    Posts:
    161
    Referrals:
    0
    Sythe Gold:
    0
    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
     
  7. wackywamba

    wackywamba Guru

    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1
    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.
     
  8. SuF

    SuF Legend
    Pirate Retired Global Moderator

    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
    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();.
     
  9. wackywamba

    wackywamba Guru

    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1
    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.
     
  10. SuF

    SuF Legend
    Pirate Retired Global Moderator

    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
    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.
     
  11. Ordou

    Ordou Active Member
    Banned

    Joined:
    Sep 11, 2011
    Posts:
    161
    Referrals:
    0
    Sythe Gold:
    0
    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] >


 
 
Adblock breaks this site