FAQ: Learn Java: Arrays - Creating an Array Explicitly

This community-built FAQ covers the “Creating an Array Explicitly” exercise from the lesson “Learn Java: Arrays”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Creating an Array Explicitly

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

public class Newsfeed {
  
  
  public Newsfeed(){
    
  }
  
  // Create getTopics() method below:
  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(topics);
    
  }
}

My solution for step 2/8

1 Like

it seems I am a bit confuse the result i got was “[Ljava.lang.String;@2aae9190” .
I thought we were suppose to see “Opinion”, “Tech”, “Science”, “Health” on the console from the System.out.println(topics);

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(topics);

}

}

ah nevermind it seems the answer to my question is explained in the next lesson :sweat_smile:

1 Like

I had this code but it would not take it
public String getTopics(){

}
any idea why

you can’t see any results when the method Newsfeed() which is empty! So remember that methods are the behavior of the class.

1 Like

I think there may be something wrong with the testing in this exercise. I think I completed tasks 1 and 2 , but it won’t give me the green checkmark and let me continue:

So, answering the question:

public static String[] getTopics(){  // this is a public method that is called getTopics(), and returns a String array
    String[] topics = {"Opinion", "Tech", "Science", "Health"};
    return topics;
  }

So it should be “yes, continue”, right?

In Fact, this code was already provided when I started the exercise:

public static String[] getTopics(){
    void;
  }

So actually, task 1. was already done, except it gives this compiler error:

Newsfeed.java:12: error: illegal start of expression
void;
^
1 error

I thought there must be some reason it’s given as static and left it like that, but the code below for testing the method is accessing it through an object, so I tried deleting the static:

public String[] getTopics(){

The output is just this:

[Ljava.lang.String;@2aae9190

  • but apparently that’s what the exercise wants, because now I got my green check mark.
    So I guess my question is:
  1. why is task 1. already done for us , but
  2. it has the unnecessary static keyword?
    …unless the purpose of this exercise is to get an understanding of how a static method belongs to a class rather than an object?

Creating an Array Explicitly is the purpose of the exercise by showing you in the return that you can initiate a new array in the return along with what we come to expect to see in the void main… It also shows you how you can assign an array to the variable with objects or other with or without methods, functions, etc. Why? No idea.

Based on the screen shot you

  • Marked as static
  • Put something in the method when it says for it to be empty on that step.
  • You made changes to the method. It doesn’t want you to create an array, it wants you to return a new array.

I had no issue (but the test on this site are notorious for reasons) with this exercise. This is what I have.

import java.util.Arrays;

public class Newsfeed {

    public Newsfeed() {

    }

    // Create getTopics() below:
    // make a method called getTopics(), which is public, returns a String array,
    // does not take any parameters.
    // create a String array called topics
    public String[] getTopics() {
        return new String[] { "Opinion", "Tech", "Science", "Health" };
    }

    public static void main(String[] args) {
        Newsfeed sampleFeed = new Newsfeed();

        String[] topics = sampleFeed.getTopics();
        System.out.println(Arrays.toString(topics));

    }
}

Based on the screen shot you

  • Marked as static

As I said, for some reason, when I first came to the exercise the method was already declared there before I even typed anything, and it already had the static keyword and was implemented void;, i.e. it wasn’t technically I who marked as static. Hence my question 2.

  • Put something in the method when it says for it to be empty on that step.

Only because the compiler complained about it being empty (as I said above), and I thought that was keeping me from getting the green checkmark for the first task. Sometimes it does happen that you do what it says in the task, but you can’t continue because the test isn’t accepting your solution - or vice versa.

  • You made changes to the method. It doesn’t want you to create an array, it wants you to return a new array.

What’s difference? Here’s what the task description says:

  1. Inside the getTopics() method, create a String array called topics and set it equal to an array containing these elements // emphasis mine

Does it not want me to create an array?

However, I now reset the whole exercise and got code that did not include the method declaration:

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

I’m guessing this is the code that the exercise is actually supposed to start with, but maybe a previous version with the wrong code is shown when it’s loaded for the first time - or maybe that happened exclusively for me.

I’ve had oddities happen here. That is my best guess on what your describing. I’ve also experienced errors.

It does, and that’s why I put the spoiler up there to show what I did and how it wanted you to do it in different ways. But yes I been here long enough to see how odd things happen along with issues with test.

Hi, I had no problem doing the tasks, but still don’t understand this line:

String topics = sampleFeed.getTopics();

If we want to call the getTopics method on the new Newsfeed instance, which is sampleFeed,

why don’t we just put: sampleFeed.getTopics(); , like we did in the previous exercises?

Also: you said in earlier units that it is impossible to use a variable that is defined outside of the method. Now, this is the exact case here as String topics was declared in another method! How could this have happened?

As I was typing this, I might have arrived at the answer, although I’m not entirely sure. Could it be that the code uses the above line because the method called (getTopics()) returns a value (an array) and values can’t stand on their own, we need to set them equal to something?
I can’t think of any other feasible explanation for why the method call doesn’t stand on its own.
Thank you, Pat