943,769 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1139
  • Java RSS
Apr 4th, 2009
0

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

Expand Post »
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
java Syntax (Toggle Plain Text)
  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
Reputation Points: 19
Solved Threads: 20
Posting Whiz in Training
joshmo is offline Offline
280 posts
since Oct 2007
Apr 4th, 2009
0

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

You can have the variable be "global". Here are 2 examples:

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

Java Syntax (Toggle Plain Text)
  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:

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

Java Syntax (Toggle Plain Text)
  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. }
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: creating "FOR LOOP"
Next Thread in Java Forum Timeline: prime numbers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC