Leandro Arruda
Leandro Arruda

Reputation: 506

Eclipse suggesting change of code. How and where I must instantiate my objects of imported class?

I was making a simple project in Java when happened something that makes me think about the use of Scanner class and the way and place where we can instantiate objects of this class.

Well, the Eclipse gave me a suggestion about how and where I must instantiate my objects of Scanner class.

I will ilustrate with a sample with a Hello World very strange:

This is a shape that I did at first moment:

import java.util.Scanner;

public class HelloWorld {

        public static void main(String[] args) {

        Scanner input = new Scanner(System.in); // Object declared inside of main method, accessing the standard input stream.

        System.out.println("Type your name: ");
        String name = input.nextLine();
        System.out.printf("%s%s", "Hello World for you, ".concat(name), "\n");
        }
 }

Then, on Eclipse i have a alert about my object, input, the instance of class Scanner. As solution he gave me a suggestion:

import java.util.Scanner;

public class HelloWorld {

        static Scanner input = new Scanner(System.in); // Object declared out of main of static form.

        public static void main(String[] args) {

        System.out.println("Type your name: ");
        String name = input.nextLine();
        System.out.printf("%s%s", "Hello World for you, ".concat(name), "\n");
        }
 }

How the way most common of make the instances of class that I import, out or inside main method, of shape static or not. Can anyone tell something about the best practices about the class Scanner and of a general form?

Upvotes: 0

Views: 133

Answers (2)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51547

Using your example, the code should look like this.

import java.util.Scanner;

public class HelloWorld implements Runnable {

    Scanner input = new Scanner(System.in); 

    public static void main(String[] args) {
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.run();
        helloWorld.close();
    }

    @Override
    public void run() {
        System.out.println("Type your name: ");
        String name = input.nextLine();
        System.out.printf("%s%s", "Hello World for you, " +
                "".concat(name), "\n");

    }

    public void close() {
        input.close();
    }
}

The main() method should do nothing but instantiate the class and execute the runnable methods. If you need to do any args processing, you can do that in the main method.

The processing should happen in class methods. I chose run(), because it's my most common class set up.

I have a default constructor that passes no parameters.

I put the Scanner definition outside the run method, because it's used in the run method and the close method. Most students forget the scanner close, so putting the close in its own method helps to remember to use it.

If the Scanner definition were only used in one method, then it should be defined in that one method.

Upvotes: 1

secario
secario

Reputation: 466

when Scanner is declared out of the main method, it's a class field, otherwise it's a local variable. Depending on if you want to use the your object in only one method (without need to call another method with scanner as a parameter) or let it be available for the whole class, you should declare it as local variable or class field.

Upvotes: 2

Related Questions