Grouping variables as classes

?

-

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?
 
R

Raymond DeCampo

- said:
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
 
W

Wibble

- said:
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.
 
W

Wibble

Raymond said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top