Search code examples
javavariables

How to access same variables in different methods?


I'm a beginner and I'm facing the same issues on different projects. I want to print the variable I got from my method I mean, for example: randomNumber method will create random number and in other method I will print that randomNumber variable on print method. Here is my code please help me. I wasn't going to ask here but I'm stuck at this and I can't understand some other things like constructors so I feel overwhelmed I though it would be nice to get some help.

package aviator;

import java.util.Random;

public class Main {
    private static int ucakrenk;
    public static void main(String[] args) {
        
        Random random = new Random();
        ucak mainucak = new ucak(7, 8, 9);
        
        ucakrenk();
        kusurat();
        test();

        }
    }

    private static void test() {
        // TODO Auto-generated method stub
        System.out.println("Siradaki ucak:");
        System.out.println("+-renk: " + ucakrenk);
    }

    private static void kusurat() {
        // TODO Auto-generated method stub
        Random random = new Random();
        int kusurat = random.nextInt(100);
        
    }

    public static void ucakrenk() {
        // TODO Auto-generated method stub
        Random random = new Random();
        int ucakrenk = random.nextInt(100);
        
    }
}

Solution

  • package aviator;
    
    import java.util.Random;
    
    public class Main {
        private static int ucakrenk;
        public static void main(String[] args) {
            
            Random random = new Random();
            ucak mainucak = new ucak(7, 8, 9);
            
            int uca = ucakrenk();
            int kusa kusurat();
            test(uca);
            test(kusa)
    
            }
        }
    
        private static void test(int ucakrenk) {
            // TODO Auto-generated method stub
            System.out.println("Siradaki ucak:");
            System.out.println("+-renk: " + ucakrenk);
        }
    
        private static int kusurat() {
            // TODO Auto-generated method stub
            Random random = new Random();
            int kusurat = random.nextInt(100);
            return kusurat
            
        }
    
        public static int ucakrenk() {
            // TODO Auto-generated method stub
            Random random = new Random();
            int ucakrenk = random.nextInt(100);
            return ucakrenk
            
        }
    }
    

    Code sample to "solve" your problem

    I encourage you to check online what is variable scope. When you create a variable in a function in java, it's scope is the function itself, hence if you don't return it it gets "deleted" (to make it simple) after the function ends

    What you need to do is "return" it (with the return keyword) and then assign it to a variable when you call the function

    ( int uca = ucakrenk(); for example)

    Then pass it as an argument to your next function to reuse it with

     private static void test(int ucakrenk) { …