• Log in or sign up for an account here: https://www.sythe.org/login

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

Joined
Sep 11, 2011
Sythe Gold
0
Messages
161
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
 
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.
 
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.

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
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
Make the fields non-static so their values can vary per instance.

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.
 
Make the fields non-static so their values can vary per instance.

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
 
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.
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
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

You need to create an INSTANCE of the class. Classname variableName = new ClassName();.
 
You need to create an INSTANCE of the class. Classname variableName = new ClassName();.

public tObject o = new tObject(500, 150, 20, 100, "images/");
public tObject t = new tObject(500, 270, 100, 20, "images/");[/code]

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.
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
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.

Oh. I'd like to see the whole code to figure it out then.
 
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
 
duelarena.gg
Back
Top