1

I'm learning Java after having just come from Haskell, and I'm trying to set up a small class to represent a generic robot. One of the instance variables that I want to add is some kind of representation of the Robot's position.

In Haskell, I would have just done something like:

type Point = (Float, Float)
data Robot = Robot { position :: Point }

but, from what I can tell, Java doesn't have any such aliasing, or tuples, so I'll have to look at it another way.

I thought of just creating another class called Point, and giving it the 2 instance variables "x" and "y" (or, ideally something a little more descriptive), but that seems clumsy. I'll have to define it in it's own file (every class is in a separate file if I correctly recall), and it will only be a few lines long (unless I need to ever add functionality, like "movement").

My other thought was to just do something like:

public class Robot {
    ...
    int xPos;
    int yPos;
    ...

}

which is messier, but more compact.

Basically, this could all be summed up as:

Is it typical in Java to separate a program into a ton of smaller pieces, regardless of the size of the piece? Or, is it typical to group things together if there's little chance of re-use?

(I know that ideally, everything is treated as though it may be re-used in the future, and cut off into a million sub-files, but to what extent should this be followed; especially during initial learning?)

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • One class per file is just a convention in Java. You can choose to ignore it (or use inner classes for that matter). – Drux Nov 23 '14 at 21:12
  • 2
    There is already a [`Point`](https://docs.oracle.com/javase/7/docs/api/java/awt/Point.html) you can use. But to answer your main question, this is primarily option based, but I would say that if a Java program _isn't_ split into "a ton" of smaller pieces then something is wrong... – Boris the Spider Nov 23 '14 at 21:12
  • @Drux I tried declaring 2 classes in the same file, and it spat out an error (Can't quote it exactly, but basically said "Class x must be in it's own file"). Unless this is only the case in the main class file. At Boris, Thanks. I guess I'll just have to get used to that. – Carcigenicate Nov 23 '14 at 21:25
  • See [related](http://stackoverflow.com/questions/2336692/java-multiple-class-declarations-in-one-file) – Drux Nov 24 '14 at 07:22

2 Answers2

1

The answer is yes :) It is typical to see very small classes in their own file and some other functionality that could be a class of its own but it isn't.

You could also see inner classes for the sake of organizing the code in some kind of hierarchy. Those inner classes are used mostly when you know that this functionality is not going to be used outside, but is too much and too different from the other part of the code to stay on the same level.

I as a rule of thumb like to stall the creation of new class up to the point when I know I need it somewhere else, but I guess it depends on the architecture of the program.

Kokozaurus
  • 639
  • 7
  • 22
  • Thanks. Just what I was looking for. I'll look-up "inner-classes". And that last point of yours might be an adjustment for me. I got in the habit of defining a data type first, then it's functionality, then linking everything together, which usually worked well. – Carcigenicate Nov 23 '14 at 21:28
  • The term "nested classes" also might be used I guess... I'm not native English speaker... – Kokozaurus Nov 23 '14 at 21:33
0

Either way is fine, you can do either. When I write code like this, I normally use the single class strategy.

public class Robot {
   ...
   int x;
   int y;
   ...
}

However, going the other way and creating a wrapper class isn't wrong either. It comes down to your preference. In this particular case, I would recommend using the built-in java.awt.Point class. To answer the general question: Both implementations are acceptable and commonly used.