Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use variables in multiple methods

 
Manny Ruiz
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manny Ruiz
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it be by putting 'static' in front if the varuable?
 
Campbell Ritchie
Sheriff
Posts: 51395
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want the same code without declaring variable as static ..please try this code

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
     
    Manny Ruiz
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 51395
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 51395
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Meee De Lune,
    Your post was moved to a new topic.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic