Jump to content
xisto Community
Sign in to follow this  
sparkx

Simple Java Question One Variable; Multiple Classes

Recommended Posts

I’m probably the only person that is able to do so much with Java3D and yet still can't even get some of the Java basics down. Anyway is it (and if so how is it) possible to make a variable editable and readable in multiple class files? It seems like this should be an easy question to answer, but here is a little code to help you understand what I am talking about.

public class test extends Applet{  public int Col = 0;  [some long code basically all it does is call for a collision behavior] [(TestB multiple times every 100 milliseconds).] if(Col==1){   [...Some collision handling...]  }}class TestB extends Behavior {  [I check my collision now I want to set the variable so it can be accessed later]  if(collision == true){  Col = 0;  }else{  Col = 1;  }}
That should give you the basics what I am doing. I’m just trying to get the collision results back into my original class not just the Behavior class.

Thanks in advanced for helping me with this problem,
Sparkx

Edit: Spacing and Smilies fixed.
Edited by sparkx (see edit history)

Share this post


Link to post
Share on other sites

You could make use of global variables but they are not always the best idea since it's easy to introduce coding problems into applications by making wide use of globals. A good alternative is to simply pass the variables to the constructors or other methods for the different classes and store the true value in a variable in main and just have the various classes modifying the original value.

Share this post


Link to post
Share on other sites

Try declaring a public variable so you can used with all of the clases, or a protected type so it can be used only by the clases that are in the same package. For example, public class startProgram{ public static int counter=0; ....... } Then when you want to call it from another class(adding 1): startProgram.counter++; This will be the same with a protected variable type, but you have to we aware that the classes were you are using the global variable are in the same package. Declaring it protected is better for security reasons(like I said before this variable is only reachable from the classes in the same package). If you need more help just write again in this topic I will review it in a time from now. Greetings, shotgun. ------------- OS:Windows Vista Ultimate Sp1 MD:Asus P5N-E CPU:2.40GHz/Intel Quad Core Q6600 RAM:Corsair Dual Channel 4GB 800Mhz VC:XFX GeForce 9800 GTX/512MB

Share this post


Link to post
Share on other sites
java3dSimple Java Question

global or not, java3d threads (behaviour) cannot talk with runtime threads. Thats why finishing your work in alive behaviours is a must not an ease!

-reply by tugalsan

Share this post


Link to post
Share on other sites

Whether a variable can be readable and editable or not is determined by its access specifier.If you specify public it can be edited and read from everywhere i.e from same package or different package. If you use default access specifier i.e when no access specifier is used with a variable like int a=1; then a can be accessed only within the same package.If you use protected access specifier i.e protected int b=1; then b can be accessed within same package and also different package but through inheritance only. private prohibits all of the above cases.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.