accessing variables from a void method in one file and using it in another

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

accessing variables from a void method in one file and using it in another

 
0
  #1
Apr 4th, 2009
I have two .java files and I want to access a variable of a method in one of the files and use it in another. However, I dont want to call the whole method and I dont want the method to return a value...I'll try to illustrate below
  1. [COLOR="Red"]//file_A.java[/COLOR]
  2. class file_A{
  3. //some code
  4. void first_method(){
  5. int variable_a=2; //variable used in this method for several things
  6. }
  7. }
  8.  
  9. [COLOR="Red"]//file_B.java[/COLOR]
  10. class file_B
  11. {
  12. //some code
  13. void second_method(){
  14. System.out.println("var"+variable_a);//ofcourse this does not work but Iam trying to do something like this
  15. }
  16. }
I hope this helps to explain my problem
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: accessing variables from a void method in one file and using it in another

 
0
  #2
Apr 4th, 2009
You can have the variable be "global". Here are 2 examples:

  1. class ClassA {
  2. public int variableA = 0;
  3.  
  4. public ClassA() {
  5.  
  6. }
  7.  
  8. public void method() {
  9. variableA = 2;
  10. }
  11. }

  1. class ClassB {
  2. public ClassB() {
  3.  
  4. }
  5.  
  6. public void methodB() {
  7. ClassA clA = new ClassA();
  8. clA.method();
  9.  
  10. System.out.println(clA.variableA ):
  11. }
  12. }

If you have a variable declared in a method you cannot access it from outside that method because it is out of scope. You need to declare it inside the class. If you declare it "private" you will need a "get" method in order to return its value.
Now if you want to access it from another class you will need of course to create an instance of the class: "ClassA" and call the method that changes its value.

But things are different when you declare it static:

  1. class ClassA {
  2. public static int variableA = 0;
  3.  
  4. public static void method() {
  5. variableA = 2;
  6. }
  7. }

  1. class ClassB {
  2. public ClassB() {
  3.  
  4. }
  5.  
  6. public void methodB() {
  7. ClassA.method();
  8.  
  9. System.out.println(ClassA.variableA):
  10. }
  11. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 520 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC