Wrapper Classes in Java for String

getoffb

Member
Jun 19, 2003
36
0
0
What is the proper way of storing a String in a wrapper class for a String. For example, I have the following two classes which are only wrapper classes for a string value:

public class ValueClass {
//holds the address
public String value;

}

public class KeyClass {
//holds the name
public String key;

public int hashCode() {
//returns the hashcode of the key
return (key.hashCode());
}
}

I then have another class, Record, which holds the key and the value:

public class Record {

private KeyClass key;
private ValueClass value;

public boolean equals(Record t) {
// comparison based only on KeyClass field
return key.equals(t.key);
}

public int hashCode() {
// depends only on KeyClass field
return key.hashCode();
}

public KeyClass getKey() {
return key;
}

public void setKey(KeyClass key) {
this.key = key;
}

public ValueClass getValue() {
return value;
}

public void setValue(ValueClass value) {
this.value = value;
}

}

When I create an instance of Record, I receive input from the user as two separate strings. How do I put the received Strings in Key and Value respectively?

This is what I thought it should be, but no joy:

KeyClass key = new KeyClass(inStrOne);
ValueClass value = new ValueClass(inStrTwo);

Record insertRecord = new Record();
insertRecord.setKey(key);
insertRecord.setValue(value);

I understand why this doesn't work, KeyClass and ValueClass do not have a constructor, which I am not allowed to have in this assignment for these two classes.

I know that this is going to be something incredibly easy, any insight/help/suggestions?
 

getoffb

Member
Jun 19, 2003
36
0
0
Nevermind, I think I figured it out by doing this:

key.key = inStrOne;
value.value = inStrTwo;

If anyone has a better way of doing this, I'm all ears.
 

ahurtt

Diamond Member
Feb 1, 2001
4,283
0
0
Originally posted by: getoffb
Nevermind, I think I figured it out by doing this:

key.key = inStrOne;
value.value = inStrTwo;

If anyone has a better way of doing this, I'm all ears.

This will work since the instance variables "key" and "value" defined in your respective classes are public. However, in general it is considered bad practice to do it this way because it breaks encapsulation - one of the key principals of OO programming. In more advanced applications, accessing instance variables in this manner also potentially opens up possibilities for thread concurrency / data race conditions when multiple threads are reading/writing the same address concurrently. A better, more "safe" way of doing it would be to do it the same exact way you did in your Record class. Restrict access to the instance variables of the KeyClass and ValueClass by making them private and make the only access to them through public getter/setter methods. Unless you have an explicit reason for making them public it is not usually good practice to do so.

EDIT: Note, you will ALWAYS have a constructor. If you do not provide a "no args" or empty constructor in your class, the Java compiler will put one there for you implicitly. If you supply your own constructor for your class, the compiler will not do this.
 

getoffb

Member
Jun 19, 2003
36
0
0
The only explicit reason I have for making them public is because that is what the assignment specified. I always thought you would use getter/setter methods like you said, but for whatever reason, we were not supposed/allowed to for this assignment.

Thanks for the help.