| | |
using a varible from main method
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
It is a matter of scope.
Put
outside the Main method (at the begining of your class.)
Look up the deffinition of scope in Java to learn more
Put
Java Syntax (Toggle Plain Text)
int number = 1
Look up the deffinition of scope in Java to learn more
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
Try this (in reference to passing the variable):
public class work {
public static void main (String [] args) {
boolean booleanAnswer;
int number = 1;
// Passes copy of number to ifstate
booleanAnswer = ifstate(number);
}
public static boolean ifstate(int passedNumber) {
if ( passedNumber == 0 )
//blah blah
}
Let me know if it is not clear
}//end class•
•
Join Date: Oct 2004
Posts: 44
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by jerbo
It is a matter of scope.
Put
outside the Main method (at the begining of your class.)Java Syntax (Toggle Plain Text)
int number = 1
Look up the deffinition of scope in Java to learn more
non-static variable thirdNo cannot be referenced from a static context
???
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by jerbo
Try this (in reference to passing the variable):
public class work { public static void main (String [] args) { boolean booleanAnswer; int number = 1; // Passes copy of number to ifstate booleanAnswer = ifstate(number); } public static boolean ifstate(int passedNumber) { if ( passedNumber == 0 ) //blah blah } Let me know if it is not clear }//end class
OK, here we go.
The reason for the boolean is you declared 'ifstate' with a return type of boolean. In reality, your "blah blah" (which I assume is shorthand for the remaining code in your method,) would contain a return statement of type boolean.
Where did you place "int number = 1;" when you placed it outside of main?
If you placed it outside of main, it should look like this:
public class work {
int number = 1; // Creates the variable outside of the class
public static void main (String [] args) {
boolean booleanAnswer;
// Passes copy of number to ifstate
booleanAnswer = ifstate(number);
}
public static boolean ifstate(int passedNumber) {
if ( passedNumber == 0 )
//blah blah
}
Let me know if it is not clear
}//end classTo declare and pass multiple parameters you separate them with a coma.
Java Syntax (Toggle Plain Text)
//Passing to a method MyMethod(1, parm2, "Parm3"); //The declared method would look like this: public void MyMethod(int p1, double p2, String p3) { // Your code here }
Note, in my example above, I passed an Integer, a Double, and a String value. Also, since I have a 'void' return type I did not need to assign anything. I just called the method by specifying the name (and passing the parameters.)
My suggestion is to start reading some more on the basic syntax for Java.
Most of what I have tried to explaine to you is very basic, and you should be able to understand it better with a good book on Java.
If you have had any VB programming the consepts are simular.
Good Luck!
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Printing
- Next Thread: paint() in java awt
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth chat class classes client code columns component data database designadrawingapplicationusingjavajslider eclipse editor error errors event exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance input integer intellij j2me java javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner screen search server set size sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads time tree windows






