0

If I had a HashMap such as in the following, how would I utilize it from another method? In this case, from Main?

public class Scratch {


public static void init() {
    WordEnums words = new WordEnums();

    List<String> bookList = new ArrayList<String>();
    for (WordEnums.Book bookValues : WordEnums.Book.values()) {
        bookList.add(bookValues.getDefinition());

    }


    HashMap<String, Object> wordDefinitions = new HashMap<>();
    wordDefinitions.put("book", bookList);


}


public static void main(String[] args) {
    List<String> book = (List<String>) wordDefinitions.get("book");

    book.stream().forEach(s -> {
        System.out.print("    ");
        System.out.println(s);
    });


}

I've tried moving it outside of init, something along the lines of what I could find here

But upon doing so, I get an error and am unable to access bookList within init.

Thanks

4
  • You'd either pass it to the method, or make it a static variable, with the caveat that making everything static may or may not be an actual good idea. Commented Feb 26, 2019 at 22:12
  • does your init method need to be static? Commented Feb 26, 2019 at 22:14
  • @molamk This is for an assignment, and the guidelines state that init should be static Commented Feb 26, 2019 at 22:16
  • Why don't you pass it as method parameter? Commented Feb 26, 2019 at 22:19

3 Answers 3

1

You can either define it as a static or create get method and get it through instance of the class it's in.

Java static: https://www.javatpoint.com/static-keyword-in-java

Sign up to request clarification or add additional context in comments.

Comments

0

Something to be aware of:

When you declare a member of a class static, it isn't associated with any instance of the class that you create. Given this information, you really have to think about how you want to structure your program.

If the method signature for init has to remain as you've written, you could make wordDefinitions a static member of your class and access it from init. However, if you do take this approach, be careful when you reference it. Remember that there is only one instance of wordDefinitions. Thus, you can only access it through referencing your class (Scratch) and not an instance of it.

Comments

0

The precise answer to the title question is "you can't". Here is why:

The variable named wordDefinitions does not exist outside of the init method. The scoping rules of Java say that it comes into existence every time you execute init and it no longer exists when init terminates. Thus accessing it from outside init has no meaning.

Similarly for bookList, which is why when you move the declaration of wordDefinitions outside of init, you have no access to bookList.

You would probably benefit from a review of Java's scoping rules. But for now, here's an approximate guide. For any data, you need to decide on its lifetime:

  1. One copy that 'always' exists independent of any instances of the class: declare it static in the class.

  2. A copy for each instance that exists for the lifetime of the instance: declare it non-static in the class (and perhaps initialize it in a constructor).

  3. Data that only exists while a method is executing: declare it in the method body.

Given the above, you can access data "upwards" in the list but not "downwards".

This glosses over the distinction between a variable and the object it refers to, but I'm trying to keep it simple.

For your current problem, if you prefer to have all static methods (which seems to be something that introductory students do - are you taught that way? If so, I don't want to confuse matters by proposing differently) you should move the declaration of wordDefinitions to class scope with a static qualifier. Loading in the book list can still be done in init.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.