Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How do I call a variable from another method?

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I call a variable from another method?

    My code has a method where the users input a bunch of variables and then those variables get added together in a new variable. What I want to know is how do I call the variable that is in the other method to another method?

    import java.util.*;
     
    public class Calc{
    	public static void main (String [] args){
    	determinevalue1();
    	determinevalue2();
    	determineTotalvalue(double value1, double value2);
    	}
     
    	public static double determinevalue1(){
    	 Scanner console = new Scanner(System.in);
    	 System.out.println("Enter your first number");
    	double v1 = console.nextDouble();
    	System.out.println("Enter your next number");
    	double v2 = console.nextDouble();
    	double vtotal = 365 * (v1 / v2);
    	value1 = 2.3 * vtotal;
    	return value1;
    	}
     
    	public static double determinevalue2(){
    	 Scanner console = new Scanner(System.in);
    	 System.out.println("Enter your first number");
    	double v1 = console.nextDouble();
    	System.out.println("Enter your next number");
    	double v2 = console.nextDouble();
    	double vtotal = 365 * (v1 / v2);
    	value2 = 2.3 * vtotal;
    	return value1;
    	}
    	public static double determineTotalvalue(){
    	double totalvalue = (value1 + value2) / 1000;
    	System.out.println(totalvalue);
    	return totalvalue;
    	}
     
    	}
    Last edited by brandon236; October 3rd, 2014 at 06:28 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I call a variable from another method?

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Show us the code and the error. Please post your code correctly using code or highlight tags per the above link.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I call a variable from another method?

    So I figured out that you can declare a new variable and assign it to the method like
    x = determinevalue1(int 3, int 4);
    but the problem with that is my values result from scanner inputs from the user so how can I do that but make it so the values are from the user?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I call a variable from another method?

    the problem with that is my values result from scanner inputs from the user so how can I do that but make it so the values are from the user?
    Huh? It sounds like you've done what you're asking for help to do.

  5. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    16
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How do I call a variable from another method?

    Uhm, declare the variables u need outside of all the methods and u can change and access them in all methods.

  6. #6
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I call a variable from another method?

    Quote Originally Posted by Resantic View Post
    Uhm, declare the variables u need outside of all the methods and u can change and access them in all methods.
    I tried that but it didn't work. It keeps saying "Cannot find symbol".

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I call a variable from another method?

    Post the code that gives that error with the full text of the error.

  8. #8
    Junior Member
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How do I call a variable from another method?

    Hello!
    You have to declare the variables to be avalible for the whole class. That means you have to declare them outside all functions and variables.
    Put all variables in the on top of the class, like this:

    public class DemonstrationExample {
        // DECLARE VARIABLES HERE, BELOW ALL METHODS AND EVENTUALLY ANY SUB CLASSES
        private static int integerNumber = 2;
        private static String text;
        private static float floatingPointNumber;
     
        // DECLARE YOUR METHODS BELOW THE VARIABLE DECLARATIONS
        public static void changeAllVariables() {
            text = "Hello World";
            integerNumber = 5;
            floatingPointNumber = 2.23;
        }
     
        public static void printAllVariables() {
            System.out.println(text + ", " + integerNumber + ", " + floatingPointNumber);
        }
     
        public static void main(String[] args) {
            changeAllVariables();
            printAllVariables();
        }
    }

    By declaring the variables below everything else in the class, they will be accesable in the whole class.

    Some keywords when declaring variables:
    public: Variables declared as public can be accessed from other classes.

    private: Variables declared as private can only be accessed from the class which it is declared in.

    static: Variables declared as static can be accessed from static methods, classes etc.

    final: Variables declared as final can not be changed.

    Hope that helps!
    /TheDDestroyer12

  9. #9

    Default Re: How do I call a variable from another method?

    - public class Practise {
    String a;

    public String getA() {
    return a;
    }
    public void setA(String a) {
    this.a = a;
    }
    void meth1(){

    a="asd";

    // System.out.println(a);



    }
    void meth2(){
    // String as=getA();
    String b="qwerty";
    System.out.println(a+""+b);

    }


    public static void main(String[] args) {

    Practise p= new Practise();
    p.meth1();
    p.meth2();
    }

    }

Similar Threads

  1. how to call an object with a string or variable?
    By Zoli in forum Java Theory & Questions
    Replies: 5
    Last Post: September 14th, 2013, 05:32 PM
  2. Is this a method call or variable declaration ???
    By hoboy in forum Java Theory & Questions
    Replies: 8
    Last Post: March 11th, 2012, 10:32 AM
  3. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  4. call super method outside of 'this'
    By questionmark in forum Object Oriented Programming
    Replies: 1
    Last Post: January 29th, 2011, 11:29 AM
  5. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM

Tags for this Thread