User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 392,047 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,300 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 3198 | Replies: 10
Reply
Join Date: Oct 2004
Posts: 39
Reputation: ultimate_fusion is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
ultimate_fusion ultimate_fusion is offline Offline
Light Poster

using a varible from main method

  #1  
Nov 24th, 2004
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: using a varible from main method

  #2  
Nov 24th, 2004
It is a matter of scope.
Put
int number = 1
outside the Main method (at the begining of your class.)

Look up the deffinition of scope in Java to learn more
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 191
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: using a varible from main method

  #3  
Nov 24th, 2004
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).
Reply With Quote  
Join Date: Oct 2004
Posts: 39
Reputation: ultimate_fusion is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
ultimate_fusion ultimate_fusion is offline Offline
Light Poster

Re: using a varible from main method

  #4  
Nov 24th, 2004
how do you "pass it to the second method as a parameter"
is it
public static int ifstate(int number)
????
Reply With Quote  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: using a varible from main method

  #5  
Nov 24th, 2004
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
Reply With Quote  
Join Date: Oct 2004
Posts: 39
Reputation: ultimate_fusion is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
ultimate_fusion ultimate_fusion is offline Offline
Light Poster

Re: using a varible from main method

  #6  
Nov 24th, 2004
not clear, i dont need a boolean , sorry to have had posted it at the top
i just need a simple number to pass
Reply With Quote  
Join Date: Oct 2004
Posts: 39
Reputation: ultimate_fusion is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
ultimate_fusion ultimate_fusion is offline Offline
Light Poster

Re: using a varible from main method

  #7  
Nov 24th, 2004
Originally Posted by jerbo
It is a matter of scope.
Put
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
???
Reply With Quote  
Join Date: Oct 2004
Posts: 39
Reputation: ultimate_fusion is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
ultimate_fusion ultimate_fusion is offline Offline
Light Poster

Re: using a varible from main method

  #8  
Nov 24th, 2004
quick question
how do you also pass(call) more than one number/char/boolean at a time?
(or all 3 types at once)
Reply With Quote  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: using a varible from main method

  #9  
Nov 24th, 2004
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.
//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!
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 191
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: using a varible from main method

  #10  
Nov 25th, 2004
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 11:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC