how to create a new map with constructor?
I am wanting to do two things
- 开发者_如何转开发
Create a private instance variable which is a map
To create an empty instance in my constructor that impliments a map and assigns it to the previous private instance variable.
The Private instance I have is
private final Map<Character, SortedSet<String>> thesaurus =
new HashMap <Character, SortedSet<String>>();
but how would create an instance variable in the constructor that would reference the private variable thesaurus upon the constructors creation.
For example
public class Book{
private final Map<Character, SortedSet<String>> thesaurus =
new HashMap <Character, SortedSet<String>>();
public Book(){
super();
/* What do i put here as an empty instance
* variable that implements a map and how
* do i assign it to thesaurus?
*/
}
It's not clear what you're asking, but here are some points:
- You can't declare an instance variable in a constructor; you have to do declare it as a member of the type (i.e. as a field).
- You can assign values to already declared instance variables in a constructor.
- You don't have to assign values to instance variables in a constructor; you can do it at the declarations.
When you write something like this:
public class Book{
private final Map<Character, SortedSet<String>> thesaurus =
new HashMap <Character, SortedSet<String>>();
//...
}
Then you've declared thesaurus
to be an instance variable of class Book
, and you also initialized its value to be a new HashMap
. Since this field is final
, you can no longer set its value to be anything else (barring reflection-based attacks).
You can, should you wish to choose so, move the initialization into the constructor. You can do this even when the field is final
(subject to various definite assignment rules).
public class Book{
private final Map<Character, SortedSet<String>> thesaurus;
public class Book {
thesaurus = new HashMap <Character, SortedSet<String>>();
}
//...
}
Something like this is done sometimes when e.g. the creation of the initial value may throw a checked exception, and therefore needs to be put in a try-catch
block.
Another option is to initialize fields in an instance initializer block:
private final Map<Character, SortedSet<String>> thesaurus;
{
thesaurus = new HashMap <Character, SortedSet<String>>();
}
And yet another option is to refactor said instance initializer block into a helper method:
private final Map<Character, SortedSet<String>> thesaurus = emptyMap();
private static Map<Character, Sorted<String>> emptyMap() {
return new HashMap <Character, SortedSet<String>>();
}
References
- JLS 8.3 Field Declarations
- 8.3.1.2
final
Fields - 8.3.2 Initialization of Fields
- 8.3.1.2
- JLS 8.6 Instance Initializers
- JLS 16 Definite Assignment
Related questions
- Initialize final variable before constructor in Java
- Proper way to declare and set a private final member variable from the constructor in Java?
- In Java, can a final field be initialized from a constructor helper?
- Java - Can final variables be initialized in static initialization block?
- Best Practice: Initialize class fields in constructor or at declaration?
You are already initializing your thesaurus variable with a map. You can move it to the constructor, like:
public class Book
{
private final Map<Character, SortedSet<String>> thesaurus;
public Book(){
this.thesaurus = new HashMap <Character, SortedSet<String>>();
}
}
There's no need to change, though. Either way, the instance field will be initialized every time an instance is created. Also note that you don't need super()
here since it's implicit.
精彩评论