Question for import Arrays in Java

so… I stuck on importing Arrays in Java. Here are the code I wrote down.

// import the Arrays package here:

public class Newsfeed {
  
  import java.util.Arrays;

  public Newsfeed(){
    
  }
    
  public String[] getTopics(){
    String[] topics = {"Opinion", "Tech", "Science", "Health"};
    return topics;
  }
  

  public static void main(String[] args){
    Newsfeed sampleFeed = new Newsfeed();
    String[] topics = sampleFeed.getTopics();
    System.out.println(Arrays.toString(topics));
  }
}

and here is the answer:

// import the Arrays package here:
import java.util.Arrays;
public class Newsfeed {
  
  
  public Newsfeed(){
    
  }
    
  public String[] getTopics(){
    String[] topics = {"Opinion", "Tech", "Science", "Health"};
    return topics;
  }
  

  public static void main(String[] args){
    Newsfeed sampleFeed = new Newsfeed();
    String[] topics = sampleFeed.getTopics();
    System.out.println(Arrays.toString(topics));
  }
}

What is the difference of putting import Arrays commend outside of class and inside of?
Help me for noob!

Welcome to the forum!

The difference is that one is allowed, and one is not.

From the Java tutorial:

To import a specific member into the current file, put an import statement at the beginning of the file before any type definitions but after the package statement, if there is one. Here’s how you would import the Rectangle class from the graphics package created in the previous section.

import graphics.Rectangle;

import statements have to be at the start of the source file. :slight_smile: