| | |
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:
Solved Threads: 19
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
I hope this helps to explain my problem
java Syntax (Toggle Plain Text)
[COLOR="Red"]//file_A.java[/COLOR] class file_A{ //some code void first_method(){ int variable_a=2; //variable used in this method for several things } } [COLOR="Red"]//file_B.java[/COLOR] class file_B { //some code void second_method(){ System.out.println("var"+variable_a);//ofcourse this does not work but Iam trying to do something like this } }
You can have the variable be "global". Here are 2 examples:
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)
class ClassA { public int variableA = 0; public ClassA() { } public void method() { variableA = 2; } }
Java Syntax (Toggle Plain Text)
class ClassB { public ClassB() { } public void methodB() { ClassA clA = new ClassA(); clA.method(); System.out.println(clA.variableA ): } }
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)
class ClassA { public static int variableA = 0; public static void method() { variableA = 2; } }
Java Syntax (Toggle Plain Text)
class ClassB { public ClassB() { } public void methodB() { ClassA.method(); System.out.println(ClassA.variableA): } }
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- File Output not Compiling (Java)
- Persistent Compiler Error on a Directory System (Java)
- JSP database connectivity according to Model View Controller (MVC) Model 2 (JSP)
- HOWTO: Share an SQL Connection between multiple forms within the same project (C#)
- Help with derived class and arithmetic in C++ (C++)
- How to delete a data structure in Builder 6.0? (C++)
Other Threads in the Java Forum
- Previous Thread: creating "FOR LOOP"
- Next Thread: prime numbers
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation binary blackberry block bluetooth character chat class classes client code component consumer database desktop developmenthelp draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaee javaprojects jmf jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner screen server set singleton size sms sort sql string swing template textfields threads time title tree tutorial-sample update windows working






