Home > Net >  Is there a way to access variables in another method?
Is there a way to access variables in another method?

Time:01-30

I'm new to java and I was wondering if there was a way to access a variable from one method in another method. While writing a program using methods, I realized that I cannot just take one variable from one method and use it in another method. So I was wondering if there was a way to do this.

Here is my code so far

import java.util.Scanner;

public class program{
    
    public static void mass(){
        double a,e,p,v;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the volume: ");
        v=scanner.nextDouble();
        p=0.8;
        System.out.println("Enter the alcohol volume:(in percents) ");
        e=scanner.nextDouble();
        e=e/100;
        a=v*e*p;
        
        System.out.println("mass is: " a);
    }
    public static void Concentration(){
        double w,r,m;
        String person;
        System.out.println("Enter the person: m for male, f for female, j for teenager ");
        Scanner sc=new Scanner(System.in);
        person=sc.nextLine();
        switch(person){
            case "m": r=0.7;
            break;
            case "f": r=0.6;
            break;
            case "j": r=0.5;
            break;
        }
        System.out.println("Enter person's weight: ");
        m=sc.nextDouble();
        //w=a/(m*r); //a from the method mass

    }
    public static void main(String[]args){
       mass();
       Concentration();
        /*If(w>=0.5){ //w from the method concentration
            System.out.println("You cannot drive!");
            Else{
                System.out.println("You can drive");
            }
        } */

    }
}

CodePudding user response:

There are two kinds of variables:

  • local variables (block-scoped)
  • member variables (class/instance level scope)

See: https://www.geeksforgeeks.org/variable-scope-in-java/

So, if you want to reuse a variable accross multiple methods, then you will need to convert it from a local variable into a member variable. In your case you only use static methods, so a would be declared outside your methods as

static double a;

and avoid declaring it inside your mass method, so your declaration line would be changed to

double e,p,v;

Note that the a is missing to avoid variable shadowing.

You may want to change your methods to instance-level methods, in which case you can declare a without the static keyword, depending on your plans and needs.

Also, a well-known approach is to implement getters and setters in order to make sure that whenever you get or set a value, if there are common operations, then they are implemented only once instead of code repeating.

Below you see the simplest changes to your code to achieve the goal you have specified:

import java.util.Scanner;

public class program{
    
    static double a;
    
    public static void mass(){
        double e,p,v;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the volume: ");
        v=scanner.nextDouble();
        p=0.8;
        System.out.println("Enter the alcohol volume:(in percents) ");
        e=scanner.nextDouble();
        e=e/100;
        a=v*e*p;
        
        System.out.println("mass is: " a);
    }
    public static void Concentration(){
        double w,r,m;
        String person;
        System.out.println("Enter the person: m for male, f for female, j for teenager ");
        Scanner sc=new Scanner(System.in);
        person=sc.nextLine();
        switch(person){
            case "m": r=0.7;
            break;
            case "f": r=0.6;
            break;
            case "j": r=0.5;
            break;
        }
        System.out.println("Enter person's weight: ");
        m=sc.nextDouble();
        w=a/(m*r); //a from the method mass

    }
    public static void main(String[]args){
       mass();
       Concentration();
        /*If(w>=0.5){ //w from the method concentration
            System.out.println("You cannot drive!");
            Else{
                System.out.println("You can drive");
            }
        } */

    }
}

Finally, you could also use a as a return value, like:

import java.util.Scanner;

public class program{
    
    public static double mass(){
        double a,e,p,v;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the volume: ");
        v=scanner.nextDouble();
        p=0.8;
        System.out.println("Enter the alcohol volume:(in percents) ");
        e=scanner.nextDouble();
        e=e/100;
        a=v*e*p;
        
        System.out.println("mass is: " a);
        return a;
    }
    public static void Concentration(double a){
        double w,r,m;
        String person;
        System.out.println("Enter the person: m for male, f for female, j for teenager ");
        Scanner sc=new Scanner(System.in);
        person=sc.nextLine();
        switch(person){
            case "m": r=0.7;
            break;
            case "f": r=0.6;
            break;
            case "j": r=0.5;
            break;
        }
        System.out.println("Enter person's weight: ");
        m=sc.nextDouble();
        //w=a/(m*r); //a from the method mass

    }
    public static void main(String[]args){
       Concentration(mass());
        /*If(w>=0.5){ //w from the method concentration
            System.out.println("You cannot drive!");
            Else{
                System.out.println("You can drive");
            }
        } */

    }
}

CodePudding user response:

To make a variable accessible in all functions of the class you can static the variable in question in the current class

package javaapplication6;

import java.util.Scanner;

// @author Vulembere

public class JavaApplication6 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    mass();
    Concentration();
    if (w >= 0.5) {
        System.out.println("You cannot drive!");
    } else {
        System.out.println("You can drive");

    }
}

static double w;

public static void mass() {
    double a, e, p, v;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the volume: ");
    v = scanner.nextDouble();
    p = 0.8;
    System.out.println("Enter the alcohol volume:(in percents) ");
    e = scanner.nextDouble();
    e = e / 100;
    a = v * e * p;

    System.out.println("mass is: "   a);
}

public static void Concentration() {
    double r, m;
    String person;
    System.out.println("Enter the person: m for male, f for female, j for teenager ");
    Scanner sc = new Scanner(System.in);
    person = sc.nextLine();
    switch (person) {
        case "m":
            r = 0.7;
            break;
        case "f":
            r = 0.6;
            break;
        case "j":
            r = 0.5;
            break;
    }
    System.out.println("Enter person's weight: ");
    m = sc.nextDouble();
    //w=a/(m*r); //a from the method mass

}

}

  • Related