944,030 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4604
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 24th, 2004
0

using a varible from main method

Expand Post »
heres the start

public class work
{
public static void main (String [] args)
{

int number = 1;
}
public static boolean ifstate()
{
if ( number == 0 )
blah blah
}

question - how do I get the second method to call on the varible from the first.

thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
ultimate_fusion is offline Offline
44 posts
since Oct 2004
Nov 24th, 2004
0

Re: using a varible from main method

It is a matter of scope.
Put
Java Syntax (Toggle Plain Text)
  1. int number = 1
outside the Main method (at the begining of your class.)

Look up the deffinition of scope in Java to learn more
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Nov 24th, 2004
0

Re: using a varible from main method

A better way is to pass it to the second method as a parameter. Especially since main is static and pulling the variable outside main would cause all kinds of other problems (especially if multiple instances of the class are created).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 24th, 2004
0

Re: using a varible from main method

how do you "pass it to the second method as a parameter"
is it
public static int ifstate(int number)
????
Reputation Points: 10
Solved Threads: 0
Light Poster
ultimate_fusion is offline Offline
44 posts
since Oct 2004
Nov 24th, 2004
0

Re: using a varible from main method

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
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Nov 24th, 2004
0

Re: using a varible from main method

not clear, i dont need a boolean , sorry to have had posted it at the top
i just need a simple number to pass
Reputation Points: 10
Solved Threads: 0
Light Poster
ultimate_fusion is offline Offline
44 posts
since Oct 2004
Nov 24th, 2004
0

Re: using a varible from main method

Quote originally posted by jerbo ...
It is a matter of scope.
Put
Java Syntax (Toggle Plain Text)
  1. int number = 1
outside the Main method (at the begining of your class.)

Look up the deffinition of scope in Java to learn more
i did that and got
non-static variable thirdNo cannot be referenced from a static context
???
Reputation Points: 10
Solved Threads: 0
Light Poster
ultimate_fusion is offline Offline
44 posts
since Oct 2004
Nov 24th, 2004
0

Re: using a varible from main method

quick question
how do you also pass(call) more than one number/char/boolean at a time?
(or all 3 types at once)
Reputation Points: 10
Solved Threads: 0
Light Poster
ultimate_fusion is offline Offline
44 posts
since Oct 2004
Nov 24th, 2004
0

Re: using a varible from main method

Quote 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 class

To declare and pass multiple parameters you separate them with a coma.
Java Syntax (Toggle Plain Text)
  1. //Passing to a method
  2. MyMethod(1, parm2, "Parm3");
  3.  
  4. //The declared method would look like this:
  5. public void MyMethod(int p1, double p2, String p3) {
  6. // Your code here
  7. }

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!
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Nov 25th, 2004
0

Re: using a varible from main method

have ever coded anything in Java before?
If not have ever read a single tutorial or beginners' book about Java?

All these questions are so basic they're part of the very earliest you should have been taught.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

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: Printing
Next Thread in Java Forum Timeline: paint() in java awt





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


Follow us on Twitter


© 2011 DaniWeb® LLC