简体   繁体   中英

How do I allow a variable to be used by various functions? java

I have tried various suggestions already given on the website.. But honestly i couldnt find something to fix it.

Basically when i try to use any variable created in Accept... it cant be used in other functions. Is there a simple solution to fix this? (Without changing the main code)

import java.util.Scanner;
class x4Salary
    {
        Scanner input = new Scanner(System.in);
        public void accept()
        {
            System.out.println("Please input the name of the teacher");
            String name = input.next();
            System.out.println("Please input the address ");
            String adress = input.next();
            System.out.println("Please input the phone number");
            long num = input.nextLong();
            System.out.println("Please input the subject specialization");
            String subjectSpecialization = input.next();
            String subS = subjectSpecialization;
            System.out.println("Please input the Monthly Salary");
            long sal = input.nextLong();
            if(sal>175000)
            {
                tax();
            }
            display();
            double tax = 0.0;
        }
        public void tax()
        {
            System.out.println("Your current salary is : " + sal);
            tax = sal + ((5/sal)*100);
            System.out.println("The salary + Tax : " +tax);
            display();
        }
        public void display()
            {
                System.out.println("The name of the teacher : " + name);
                System.out.println("The Address of the teacher :" +adress);
                System.out.println("The Phone number of the Teacher: "+num);
                System.out.println("The Subject Specialization of the Teacher" + subS);
                System.out.println("The Monthly salary of the teacher" +sal + tax);
            }
        }

You can make those variables as class members

class x4Salary
{
    protected String name, address; //and so on

and then instead of String name = input.next();

 name = input.next();

name would be visible in all methods of X4Salary

You would need to make those variables as instance variables.

The variables created in a method, in confined to that method scope only. It is not visible outside that method. It will be created when the method is invoked, and then when the method is finished with execution, that variable becomes eligible for Garbage Collection .

This is true for any block scope you have. The variables created in one block, will not be visible outside that block.

For eg: -

{   // A block
    int num = 10;
}

System.out.println(num);  // Error. num not visible here.

So, to access the variable in all methods, declare it as: -

public class Demo {
    private int num;  // Instance variable declaration. Outside every method.

    public void setNum(int num) { 
        this.num = num; 
    }

    public int getNum() { 
        return this.num;  
    }
}

So, as you can see, variable num is used in both the methods: - setNum and getNum .


Go to this tutorial to get started with classes , objects , and member declarations : -

you cant use the local variables(variables defined inside your method) outside of that method. they are only confined to that method . you either have to make it an instance variable or return that variable so that the other methods can use it. in your case. if you want to use sal outside method accept . make it an instance variable.

Class classname {
public long sal;

public void accept(){
//can access sal here
 sal = input.nextLong();
} 

public void display(){
//cvan access sal here
}
}

Those variables are scoped to the method alone . If you want to retain those values entered your options are:

  1. create member variables in the containing class and populate those. Those values would be available for the lifetime of that class
  2. return the values from the method. Since you have multiple values you would likely want an object containing these values to be returned
  3. call another object from within that method, and pass the data that way.

For option 2 you could define a return object thus:

class Teacher {
    private String name;
    private String phone;

    // add appropriate constructor
}

I suspect Teacher is a key object in your application and you would likely want to add the method display() to the Teacher class itself. Remember that OO is about creating objects and getting them to do things for you, not handling discrete related variables yourself .

Define your variables as Member Variables in the Class then it can be accessible in all the member methods.

import java.util.Scanner;
class x4Salary
    {
        private double tax=0.0;
        private string name, adress, subS;
        private long num, sal;
        Scanner input = new Scanner(System.in);
        public void accept()
        {
            System.out.println("Please input the name of the teacher");
            name = input.next();
            System.out.println("Please input the address ");
            adress = input.next();
            System.out.println("Please input the phone number");
            num = input.nextLong();
            System.out.println("Please input the subject specialization");
            subS = input.next();
            System.out.println("Please input the Monthly Salary");
            sal = input.nextLong();
            if(sal>175000)
            {
                tax();
            }
            display();
        }
        public void tax()
        {
            System.out.println("Your current salary is : " + sal);
            tax = sal + ((5/sal)*100);
            System.out.println("The salary + Tax : " +tax);
            display();
        }
        public void display()
        {
             System.out.println("The name of the teacher : " + name);
             System.out.println("The Address of the teacher :" +adress);
             System.out.println("The Phone number of the Teacher: "+num);
             System.out.println("The Subject Specialization of the Teacher" + subS);
             System.out.println("The Monthly salary of the teacher" +sal + tax);
        }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM