-1

I have this code:

Scanner scan = new Scanner(System.in);
Random rand = new Random();
int num1 = rand.nextInt(100);
int num2 = rand.nextInt(100);

What I am asking here is how can I get these into another method if they are created in a different method for example.. I create method 'x' with that code in, I then create another method 'y' which need to use these variables inside of 'x' method Am I trying something that can't be done? Or should I be using objects/classes or whatever

Please help.

mleko
  • 11,650
  • 6
  • 50
  • 71
  • objects by returning values from the method, if it was c you could use pointers or make the variables global in java – jgr208 Dec 09 '14 at 14:45
  • I had a feeling it would be objects just was hoping there was a way as didn't wanna change all my code lol. Thanks tho – user4341854 Dec 09 '14 at 14:46
  • [Methods in Java](https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html) – khelwood Dec 09 '14 at 14:46
  • then make them global – jgr208 Dec 09 '14 at 14:46
  • NO, don't make them global. Object oriented programming is about encapsulation and abstraction. I see no abstraction here - just C code written in Java. – duffymo Dec 09 '14 at 14:47
  • @duffymo I know, but if he doesnt want to make them objects that is the only way around it – jgr208 Dec 09 '14 at 14:49
  • I don't care what he wants. Learn to write object oriented code properly. It's the wrong path for sure. – duffymo Dec 09 '14 at 14:57
  • @duffymo i know so i did another way with making a private class and using getter and setters in there to set the numbers – jgr208 Dec 09 '14 at 14:59

1 Answers1

0

The only way around making them objects is as follows

class foo
{
int num1
int num2

public void someMethod()
{
Scanner scan = new Scanner(System.in);
Random rand = new Random();
num1 = rand.nextInt(100);
num2 = rand.nextInt(100);
}
}

That would make them global variables which can be accessed from anywhere inside the class without having to pass around objects. However is not the best practice to use. The best practice would be to make an objects which sets num1 and num2 and returns the result of changing the numbers.

That would be done as follows

public class foo
{
       TwoNumbers t = new TwoNumbers();

    public void someMethod()
    {
    Scanner scan = new Scanner(System.in);
    Random rand = new Random();        
    int num1 = rand.nextInt(100);
    t.setone(num1);
    int num2 = rand.nextInt(100);
    t.settwo(num2);
    }
    public void bar()
    {
     t.getone();
     t.gettwo();
    }
}

private static class TwoNumbers {
    private Integer num1;
    private Integer num2;

    public void setone(int a) {
        this.num1 = a;
    }

   public settwo(int b)
{
       this.num2 = b;
}

   public int getone()
   {
     return this.num1;
   }

   public int gettwo()
   {
     return this.num2;
   }
}

If you want to keep having to get new numbers from a different method in the class here is how you would do that

public void bar()
{
 while(true) //don't know what condtion you need
 {
  someMethod();
  t.getone();
  t.gettwo();
 }
}
jgr208
  • 2,896
  • 9
  • 36
  • 64