How do you share a scanner in java, across classes?

So I've got something simple that works kinda like this:

Class 1  
{
      public static void main(String[] args)
     {
	Scanner in = new Scanner(System.in);
	Scanner newIn = new 2(in);
     }
}

Then for 'class 2'

Class 2
{
  private Scanner _scanner;
  public static void setName(Scanner _scanner)
    {        
        _scanner = newIn;
    }
}

As you may have guessed this is just dummy/example code, and if there's any really stupid errors, it's because I'm so tired, I'm not just out right stupid, or at least I hope not. I mean I've been doing very, very silly mistakes due to a lack of sleep, such as forgetting to include ";" at the end of a line, I mean come on, not even people who've been doing it for less than a week do that?

I know I'm new to Java, but I'm not so new that I should keep forgetting such basic things, anyway, I hope you guys can help, any form of help would be appreciated.

class two is wrong. class 1 is also wrong but thats a different point.

the line in setName within Class 2 should be
this._scanner = _scanner;
and in Class 1 you should not have newIn be equal to a new 2 UNLESS Class 2 is an extension of Scanner (which it doesnt seem to be)

I see, I'll try that out now, I am new to Java, like this is the first time I'm working with multiple classes tbh, but I'm not so new that I can't do stuff like simple if statements or loops, simple and real beginner stuff I'm familiar with!

Okay, so I've sorta solved that problem but I now have a new problem.

Here's an example:

Class ClassOne 
{
    public static Double number;
   public static Double number2;
    public static Double numbers(){
         // I have imported the correct classes, like sqrt
         number = rnd.nextDouble() * 5;
         number2 = Math.sqrt(number * number 
         * number);

         return number2;
    }
}

Now I want to be able to access this and use it for one of my methods in another class.

Class ClassTwo 
{
    System.out.println(number2)
}

If I try something like this, or very similar I get an error message saying something like Null Pointer Exception, any advice?

BTW. I should also note, if I declare the number before hand, like number2 = 90, it works, but it's always equal to 90 then, it doesn't work like it should.

You need to really learn how to deal with variables before you start passing data between two classes. Get it so that you only deal with one class and get comfortable with that. Then moving to two, three, or n number of classes is easy

So let's start with passing data between methods

My Java is a bit rusty but I'll try to critique:

  • The class keyword needs to be lowercase.
  • You're using invalid names for your classes IIRC (it won't compile at all I think). You should rename them to One and Two.
  • In your main in class one, you create an instance of your second class and place it in newIn. The type of the variable is incorrect; You can't put the instance of your second class in a variable which is supposed to contain a Scanner instance.
  • You need to define a constructor for your second class which accepts the Scanner instance.
  • The setName method is static; this won't allow you to change the private _scanner variable on class instances. I think you need to read on what the static keyword does.
  • In class two, you've used newIn, but that variable isn't declared anywhere in the scope of that class.
  • When you set the _scanner variable in class two, you're actually setting the argument which is passed into the method, not the private field.

Here's the corrected code: