ppc
ppc

Reputation: 77

Can we write this without an import statement by any chance? Just wondering

I always tend to write the import statement whenever i am writing a program so Im just wondering if i can write it differently without the import and still compiles. Thanks

import java.util.Scanner;

class kk {

    public static void main (String args){
        Scanner s = new Scanner (System.in);
    }

}

Upvotes: 1

Views: 139

Answers (1)

GBlodgett
GBlodgett

Reputation: 12819

Yes you can. Import statements just allow you to use the shorthand name for the Object. You can just specify the whole name:

java.util.Scanner in = new java.util.Scanner(System.in);

However there isn't really any reason why not to use the import.

From the docs tutorial:

However, if you are trying to use a member from a different package and that package has not been imported, you must use the member's fully qualified name, which includes the package name.

Upvotes: 2

Related Questions