1

I have a quick Android development question. I am working on an exercise and I have a class that gets the current date and time. The exercise wants me to try and format the date's text to something in a more readable human form. This is what I had which returned a date timestamp.

mDateButton.setText(getDate().toString());

So then to format the returned date to a more readable format this is what I did, with the help of a Google search.

mDateButton.setText(DateFormat.format("EEEE,  MMM d, yyyy", mCrime.getDate()).toString());

And it works, but my question is before getting the answer from a google search, how would I have known to put the DateFormat class before the returned value of my method for getDate(). I was putting it after the getDate() call. Is there some order I should be learning with OOP, because this is where I get stuck all the time.

Psy Chotic
  • 219
  • 1
  • 3
  • 11

1 Answers1

0

This is an example of the difference between class methods and instance methods.

format(CharSequence inFormat, Date inDate) is an example of a class method, technically called a "static method", which is why it is called on the DateFormat class.

getDate() is an example of an instance method, which is why it is called on the object mCrime (an instance of the Crime object).

Matt Logan
  • 5,886
  • 5
  • 31
  • 48
  • Thanks Matt, so in other words class methods have precedence over instance methods, if I am understanding this right. – Psy Chotic Oct 05 '13 at 19:49
  • Not quite. Class methods (technically called "static methods") are called on the class itself, and don't require an actual instance of that class. On the other hand, instance methods are called on objects that are initialized (or "instantiated") instances of a class. Here's a deeper discussion of the difference: http://stackoverflow.com/questions/11993077/difference-between-static-methods-and-instance-methods – Matt Logan Oct 05 '13 at 20:00