wood burning stoves*
The moose likes Beginning Java and the fly likes how to use variables in multiple methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to use variables in multiple methods" Watch "how to use variables in multiple methods" New topic
Author

how to use variables in multiple methods

Manny Ruiz
Ranch Hand

Joined: Dec 01, 2010
Posts: 40
Is there a way to create variables that can be used by multiple methods within then same class. i have been having a hard time figuring this out. I recently finished Java programming I at UOPx, and I'm still not sure how I pulled that off. An example of what I am asking is if I create a main method to calculate a mortgage payment amount, then created another method to create an amortization table, how can I use the variables created in the main method in the amortization table? This one has been bugging me for a while.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Manny,
Welcome to CodeRanch!

A static variable can be accessed by the main method and any other method in the class. An instance variable can be accessed by any non-static method in the class.

In the future, try to choose a more meaningful subject line. I've changed yours to "how to use variables in multiple methods" from "total newbie question". That helps people quickly decide whether to open the thread.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Manny Ruiz
Ranch Hand

Joined: Dec 01, 2010
Posts: 40
Thank you, I am looking forward to learning more here.

So what you are saying is if I create the variables in the class, I can use them anywhere? for example

Public class example

int a =1
int b =2

public static void main (String[] args){

int c= a +b

}

public static void minus(){

int d = a-b
}
}

In this case, would both methods be able to use the values of both a & b ?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

One more suggestion on posting. Next time you post code - please use code tags ([code][/code] or press the Code button at the top of the message editor). It makes the code a lot easier to read.


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Manny Ruiz wrote:

In this case, would both methods be able to use the values of both a & b ?


This is almost correct. Both methods are static - which means you are working "in a static context." Since you are working in the static context then the variables need to be in a static context as well. How do you think you could do that?
Manny Ruiz
Ranch Hand

Joined: Dec 01, 2010
Posts: 40
Would it be by putting 'static' in front if the varuable?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38064
    
  22
More likely you should move as much code out of the main method as you can, and create an instance of your class. Then you can get away from the static context.
By the way: "example" is a poor choice for a class name, and it should be spelt "Example" with a capital E.
saravanan ragunathan
Ranch Hand

Joined: Aug 02, 2010
Posts: 84
if you want the same code without declaring variable as static ..please try this code


"I Love Java Ranch"
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

To help sort this out, note that there are two big ideas here:
  • Scope
  • Instance vs. static


  • First, "scope" establishes where elements can be accessed. In general, scope is delimited by braces, {}, so a variable's scope is inside the braces in which it's declared. For example...

    (Note: This is one reason that clean, consistent formatting is so appreciated! )

    Second, the keyword "static" associates members with the class. Without "static," a member is associated with an instance (object) of that class. For example...


    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    Manny Ruiz
    Ranch Hand

    Joined: Dec 01, 2010
    Posts: 40
    I think I am starting to understand a little better. I guess what confuses is me knowing which way is better to use. In the begining class I just completed, we created a simple mortgage payment calculator. I had all my variable in the main method as well as my calculation, and everything worked fine. I got confused when we were told to create a new method to hold calculations. I ended having to create double variables. How could i have done this without having to double up on variables.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38064
        
      22
    You will have to move all that out of the main method, particularly if you are using a GUI.
    You will need methods to set the principal, the term and the interest rate, into a Mortgage object. Or, probably better, pass those values to its constructor. Then a calculate method which returns the payment.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38064
        
      22
    Meee De Lune,
    Your post was moved to a new topic.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: how to use variables in multiple methods
     
    Similar Threads
    selection sort in a linked list
    printing from outside main()
    Mortgage Calculator and GUI
    How a static main method, calls other non-static member methods of its class?
    Method used at Runtime