Grouping variables as classes

Discussion in 'Java' started by -, Jul 14, 2005.

  1. -

    - Guest

    If I have a class that has, among others, several fields such as

    public class SomeClass {

    private long startTime...
    private long endTime...

    // some other fields here

    ...

    public SomeClass() {
    }

    public long getStartTime() {
    return startTime;
    }

    ...
    }

    is it a good idea if I do this:


    public class SomeClass {

    private TimeInfo timeInfo = new TimeInfo();

    // some other fields here

    ...

    public SomeClass() {
    }

    public TimeInfo getTimeInfo() {
    return timeInfo;
    }

    protected static class TimeInfo {

    private long startTime ...
    private long endTime ...

    ...

    public TimeInfo() {
    }

    public long getStart() {
    return start;
    }

    ...
    }
    }

    This way, I can use 'getStart()' and 'getEnd()' rather than
    'getStartTime()' and 'getEndTime()'.

    Anything I should know of?
     
    -, Jul 14, 2005
    #1
    1. Advertisements

  2. - wrote:
    > If I have a class that has, among others, several fields such as
    >
    > public class SomeClass {
    >
    > private long startTime...
    > private long endTime...
    >
    > // some other fields here
    >
    > ...
    >
    > public SomeClass() {
    > }
    >
    > public long getStartTime() {
    > return startTime;
    > }
    >
    > ...
    > }
    >
    > is it a good idea if I do this:
    >
    >
    > public class SomeClass {
    >
    > private TimeInfo timeInfo = new TimeInfo();
    >
    > // some other fields here
    >
    > ...
    >
    > public SomeClass() {
    > }
    >
    > public TimeInfo getTimeInfo() {
    > return timeInfo;
    > }
    >
    > protected static class TimeInfo {
    >
    > private long startTime ...
    > private long endTime ...
    >
    > ...
    >
    > public TimeInfo() {
    > }
    >
    > public long getStart() {
    > return start;
    > }
    >
    > ...
    > }
    > }
    >
    > This way, I can use 'getStart()' and 'getEnd()' rather than
    > 'getStartTime()' and 'getEndTime()'.
    >
    > Anything I should know of?


    Why don't you just use the shorter names in the original class?
    Unless TimeInfo is re-used somehow this appears to be a pointless change.

    Ray

    --
    XML is the programmer's duct tape.
     
    Raymond DeCampo, Jul 14, 2005
    #2
    1. Advertisements

  3. -

    Wibble Guest

    - wrote:
    > If I have a class that has, among others, several fields such as
    >
    > public class SomeClass {
    >
    > private long startTime...
    > private long endTime...
    >
    > // some other fields here
    >
    > ...
    >
    > public SomeClass() {
    > }
    >
    > public long getStartTime() {
    > return startTime;
    > }
    >
    > ...
    > }
    >
    > is it a good idea if I do this:
    >
    >
    > public class SomeClass {
    >
    > private TimeInfo timeInfo = new TimeInfo();
    >
    > // some other fields here
    >
    > ...
    >
    > public SomeClass() {
    > }
    >
    > public TimeInfo getTimeInfo() {
    > return timeInfo;
    > }
    >
    > protected static class TimeInfo {
    >
    > private long startTime ...
    > private long endTime ...
    >
    > ...
    >
    > public TimeInfo() {
    > }
    >
    > public long getStart() {
    > return start;
    > }
    >
    > ...
    > }
    > }
    >
    > This way, I can use 'getStart()' and 'getEnd()' rather than
    > 'getStartTime()' and 'getEndTime()'.
    >
    > Anything I should know of?

    Its a fine pattern, particularly if the nested class has meaning, like a
    time range. Consider initializing timeinfo like:

    private TimeInfo timeInfo = newTimeInfo();
    protected TimeInfo newTimeInfo() { return(new TimeInfo()); }

    so that subclasses can override.
     
    Wibble, Jul 14, 2005
    #3
  4. -

    Wibble Guest

    Raymond DeCampo wrote:
    > - wrote:
    >
    >> If I have a class that has, among others, several fields such as
    >>
    >> public class SomeClass {
    >>
    >> private long startTime...
    >> private long endTime...
    >>
    >> // some other fields here
    >>
    >> ...
    >>
    >> public SomeClass() {
    >> }
    >>
    >> public long getStartTime() {
    >> return startTime;
    >> }
    >>
    >> ...
    >> }
    >>
    >> is it a good idea if I do this:
    >>
    >>
    >> public class SomeClass {
    >>
    >> private TimeInfo timeInfo = new TimeInfo();
    >>
    >> // some other fields here
    >>
    >> ...
    >>
    >> public SomeClass() {
    >> }
    >>
    >> public TimeInfo getTimeInfo() {
    >> return timeInfo;
    >> }
    >>
    >> protected static class TimeInfo {
    >>
    >> private long startTime ...
    >> private long endTime ...
    >>
    >> ...
    >>
    >> public TimeInfo() {
    >> }
    >>
    >> public long getStart() {
    >> return start;
    >> }
    >>
    >> ...
    >> }
    >> }
    >>
    >> This way, I can use 'getStart()' and 'getEnd()' rather than
    >> 'getStartTime()' and 'getEndTime()'.
    >>
    >> Anything I should know of?

    >
    >
    > Why don't you just use the shorter names in the original class?
    > Unless TimeInfo is re-used somehow this appears to be a pointless change.
    >
    > Ray
    >

    Yeah, I dont understand the name change either, especially since the
    long names are more clear. Its generally a bad idea to have more than
    one name for anything. Saving typing is a bad excuse.

    If the containing class is the only consumer, particularly if the
    semantics are local, then it can make sense to nest. It can make code
    more clear and makes it easy to promote the class to standalone later.
     
    Wibble, Jul 14, 2005
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David

    Classes within classes

    David, Jul 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    5,185
    David
    Jul 22, 2005
  2. lonelyplanet999
    Replies:
    1
    Views:
    2,480
    VisionSet
    Nov 13, 2003
  3. Carfield Yim
    Replies:
    1
    Views:
    1,954
    Andrew Thompson
    May 31, 2004
  4. Razvan
    Replies:
    11
    Views:
    1,107
    Andrew Thompson
    Jul 17, 2004
  5. Razvan
    Replies:
    5
    Views:
    11,621
    Dale King
    Jul 27, 2004
  6. kelvSYC
    Replies:
    2
    Views:
    722
    Chris Smith
    Aug 18, 2004
  7. Dilip

    Grouping global variables

    Dilip, Feb 15, 2007, in forum: C++
    Replies:
    6
    Views:
    427
    Gavin Deane
    Feb 15, 2007
  8. Replies:
    9
    Views:
    1,294
Loading...