0

I am looking to write a small application that reads information from an excel file, which we will use to update an xml.

I'm pretty well-verse in .net, where I would just simply create a new class with some properties to hold all of the values I am pulling from the excel file

Public Class HouseNoteInfo
    Private _MessageText As String

    Public Property MessageText() As String
        Get
            Return _MessageText
        End Get
        Set(ByVal value As String)
            _MessageText = value
        End Set
    End Property

    Public Sub New(ByVal msgText As String)
        _MessageText = msgText
    End Sub
End Class

And then I would simply create a list of the class, which I would be able to effectively keep data ordered and would be able to pull which items I wanted

Public NoteList As New Generic.List(Of HouseNoteInfo)
...
NoteList.Add(New HouseNoteInfo(msgText))

I've been trying to find an alternative for Java, but I don't believe they have the same getter/setter method as .net so I've only found I can create a HashMap of a new class containing my own methods to get and set the values but it feels clunky to me and was just wondering if there was a more efficient way to go about this?

Criel
  • 805
  • 1
  • 14
  • 32
  • I believe I don´t understand well your question, that code have a direct translation in Java in exactly the same way – Francisco Hernandez Dec 02 '15 at 15:03
  • Java doesn't have properties in the way that C# supports. You'd either have to use getters and setters or throw encapsulation aways and directly access the fields (make them public). – Thomas Dec 02 '15 at 15:05

3 Answers3

1
public class HouseNoteInfo {

    private String messageText;
    private boolean enabled;  // to demonstrate getter/setter for booleans

    // constructor
    public HouseNoteInfo(String messageText) {
        this.messageText = messageText;
    }

    // getter
    public String getMessageText() {
        return messageText;
    }

    // setter
    public void setMessageText(String text) {
        this.messageText = text;
    }

    // getter for boolean prefixed with "is" instead of "get"
    public boolean isEnabled() {
        return enabled;
    }

    // setter
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

}

List<HouseNoteInfo> list = new ArrayList<>();
list.add(new HouseNoteInfo("message text"));
jabu.10245
  • 1,884
  • 1
  • 11
  • 20
1

Java doesn't have a property concept like that, but you can do the same with fields and methods. This would basically be the same:

public class HouseNoteInfo{

    private String messageText;

    public String getMessageText() {
        return messageText;
    }

    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    public HouseNoteInfo(String messageText) {
        this.messageText = messageText;
    }
}
André Stannek
  • 7,773
  • 31
  • 52
1

You can use in this way in java

public class HouseNoteInfo {

    private String messageText;

    public HouseNoteInfo(String msg) {
        this.messageText = msg;
    }

    /**
     * @return the messageText
     */
    public String getMessageText() {
        return messageText;
    }

    /**
     * @param messageText the messageText to set
     */
    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }   
}

and then

final List<HouseNoteInfo> noteList = new ArrayList<>();

...

noteList.add(new HouseNoteInfo(msg));
Francisco Hernandez
  • 2,378
  • 14
  • 18