Hi there I am reading some java tutorial and a have a question about these 2 classes java.util.Scanner; javax.swing.JOptionPane;
In the tutorial I am looking at the 2 classes are used in separate programs to get the input from a user
With the first class http://www.homeandlearn.co.uk/java/user_input.html after importing the class we create a new object:
Scanner user_input = new Scanner( System.in );
and then move on and get the input from the user
But if we want to use the second class as in here http://www.homeandlearn.co.uk/java/java_option_panes.html, no object is created we use it directly with a nethod without creating an instance showInputDialogue():

String first_name;
first_name = JOptionPane.showInputDialog("First Name");

Why is that? I thought that a class on its own doesn't do anything till we create an instance of the object. Please bear in mind this is my second lesson of java, so I might be missing something really obvious here

ehm ... no, if it's a static method/member, you don't need an instance of the class. you can call it through an instance, but that would be confusing and might make you believe that the value of the static member depends on the instance, which it doesn't.

static means: class scope, not instance scope.
for instance, if you have a class Person, and that has a static member name;

if you create a hundred instances, each time you set the name for one instance, you actually set it for all of them, since it's linked to the class, not to the instance.

uhm...so how do I know that a method is static? Take the example above and the 2 classes again java.util.Scanner; vs javax.swing.JOptionPane; how do I know that the method used in the first class isn't static and the method used in the second one is?
thanks

You have a clue in how it's called - if its called using an instance then its (probably) an instance method. If it's called using a class name then its definitely static.
In the end you can always look at the API documentation which shows every method's details.

Ah ok, I think I got it : - ), thanks

Sorry to reopen the discussion, but I came across an intereting example, this program:

import static java.lang.System.out;
import java.util.Scanner;
import java.util.Random;

class GuessingGame {

    public static void main(String args[]) {
        Scanner myScanner = new Scanner(System.in);

        out.print("Enter an int from 1 to 10: ");

        int inputNumber = myScanner.nextInt();
        int randomNumber = new Random().nextInt(10) + 1;

        if (inputNumber == randomNumber) {
            out.println("**********");
            out.println("*You win.*");
            out.println("**********");
        } else {
            out.println("You lose.");
            out.print("The random number was ");
            out.println(randomNumber + ".");
        }

        out.println("Thank you for playing.");
    }
}

Here are the interesting lines:

        int inputNumber = myScanner.nextInt();
        int randomNumber = new Random().nextInt(10) + 1;

Now,the first one is an instance method isn't it, because we had to create an object first and then call the method on the new object
With the second I don't quite understand. We don't seem to be creating a new object as we've done previously, but we call the method nextInt directly when we create the new method. Sorry maybe the syntax is a bit new to me.
So is the second a static method?
thanks

new Random() creates a new instance of Random, then we use that to call nextInt. So nextInt is an instance method.

I see, thanks for that. One more thing, shouldn't we create a variable of random type rather than int? Like

Random randomNumber = new Random();
int theNumber = randomNumber.nextInt(10) + 1;

rather than int randomNumber = new Random().nextInt(10) + 1;
thanks

Unless you want to use the new Random again there's no need to create a variable

ok thanks for that!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.