• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

how to use variables in multiple methods

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would it be by putting 'static' in front if the varuable?
 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you want the same code without declaring variable as static ..please try this code

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 76395
    364
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 76395
    364
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Meee De Lune,
    Your post was moved to a new topic.
     
    I am mighty! And this is a mighty small ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic