Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Simple Java Question, One Variable; Multiple Classes
sparkx
post Jun 4 2008, 07:05 PM
Post #1


Sparkx
Group Icon

Group: [HOSTED]
Posts: 346
Joined: 11-October 06
From: Dana Point, CA, USA
Member No.: 16,496



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.
CODE
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.

This post has been edited by sparkx: Jun 4 2008, 07:07 PM
Go to the top of the page
 
+Quote Post
Jeigh
post Jun 5 2008, 05:28 PM
Post #2


Whitest Black Mage
Group Icon

Group: [MODERATOR]
Posts: 1,325
Joined: 20-May 05
From: NB, Canada
Member No.: 5,281



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.
Go to the top of the page
 
+Quote Post
shotgun
post Aug 25 2008, 01:24 AM
Post #3


Newbie [ Level 2 ]
Group Icon

Group: [HOSTED]
Posts: 24
Joined: 16-August 08
Member No.: 32,092



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
Go to the top of the page
 
+Quote Post
magiccode9
post Aug 25 2008, 06:02 AM
Post #4


Member [ Level 2 ]
Group Icon

Group: [HOSTED]
Posts: 61
Joined: 7-November 05
Member No.: 9,489



Hi, How about if the startProgram.counter be able modify by different threads.
How do I sync it correctly ?

Do I need to place this call in a method configured as synchronized ?

Thanks,

P.S. I'am new to java and still learning it.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. java.lang.NullPointException(4)
  2. Java Unlimited(14)
  3. Array Sorting(21)
  4. What Are The Advantages Of Java Vs C++?(15)
  5. Need Help: Find Lowest Character Using Java(7)
  6. Download Java Ebooks(13)
  7. Java By Example(8)
  8. Video Streaming In Web Browser Through Java Or Jsp(1)
  9. Looking For A Java IDE(25)
  10. On Why Java Is 'c'ooler!(10)
  11. Other Sound Format Support(3)
  12. Java Phone Book(2)
  13. How To Create Exe File In Java?(12)
  14. How Do I Test A Java Aplication(11)
  15. Mozilla And Java!(2)
  1. Need To Modify Xml Attribute Using Java(4)
  2. Bluetooth And Java(5)
  3. Enumeration Enum = .....(4)
  4. Java Sdk Vs. Java Jdk?(2)
  5. Graphcal User Interfaces In Java(4)
  6. Java Db Help Pls(2)
  7. Loading 3d Models In Java?(2)
  8. Java Applet Loading Error(5)
  9. Setting Up Java Correctly(8)
  10. Java Java.security.accesscontrolexception(6)
  11. Use Of Xml Properties File For One Key - Multiple Value Mapping(0)
  12. Java And Sql: Data Mismatch(6)
  13. Java Memory Leak?(0)


 



- Lo-Fi Version Time is now: 8th September 2008 - 03:27 AM