Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


[java] variable access question

This topic is 6089 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

im having trouble accessing a variable in another class, you would think the following code would work but it doesnt. getTrack is in a class, and getTrack just returns a private integer. all i want to do is set the private variable in getTrack to 0 myTracks[x].getTrack()=0;

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
It does not work because the getTrack method returns a copy of the private integer. Take a look at the code:

int y = someObject.getTrack(); // copies your private integer into y
y = 0; // changes the value of y, but does not affect any values in someObject

The correct way to achieve the result you want is to add a setTrack method to the same class as the getTrack method. So if the private integer that getTrack returns is called track, then such a method would look like this:

public void setTrack(int n) {
track = n;

and your code would look like:


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!