The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Java class versus C++ struct layout

I'm an experienced c++ programmer doing a java project for the first time.

I'm just wondering how an experienced Java programmer would setup/layout the following class/structrue (disregard any syntax errors - just looking for file layout issues):

I want to know is it better to include obvious C++ structs in separate Java .class files or to make them subclasses in the main.class file.

------

in C++:

class myProject {

  struct {
    int i;
    int j;
  } myStuct[200];

}

------

In java:

Should I make two separate classes with separate .class files like

struct.class file:
class {
  int i;
  int j;
};

project.class file:
class {
  struct[] mystruct = new struct();
}


OR

project.class file:
class {

  class {
    int i;
    int j;
  } struct;

  struct[] mystruct = new struct();
}


Is it good design to have a separate .clas file for every single class in your project?

Is it just dependent on whether 1 OR more than 1 main classes need to use it?

Monday, October 04, 2004
 
 
In Java it's normally one class one file, I believe the compiler complains if you do not. The structure you are suggesting though is called an inner class or nested class and can be done but is usually considered harder to read but obviously if you want to do it you can. I myself would put then in separate files unless the inner one was super simple (which is usually when inner classes aren't minded as much).
Justin Kolb Send private email
Monday, October 04, 2004
 
 
public class MyClass {
...
}

must be stored in MyClass.java - this is a requirement. Non-public classes may be stored which ever way you want, but I would still put them in their own file.

For your struct you could use an inner class:

public class MyClass {
  class MyStruct {
      int i;
      int j;
      int k;
  }
  ...
}

but again, I wouldn't do this without a good reason.
Dino
Monday, October 04, 2004
 
 
Just a note you don't have to buy any books to learn Java alot of good info can be found in the Java Tutorial when you need to look something up. The only thing you have to know is the terminology to use which might be new to you. Since I've been learning C++ recently after using Java for years you should have no problems it's really just a toned down and simplified C++.

http://java.sun.com/docs/books/tutorial/index.html
Justin Kolb Send private email
Monday, October 04, 2004
 
 
More ...

Also, think about using a dynamic type of array instead of the static one.

public class MyClass {
  class MyStruct {
      ...
  }

  Vector structVector;

}

If you're dealing with a fixed size you're fine with your array.

If your struct element has a meaning on its own, outside its parent class, then implementing it as an inner class is wrong.
Dino
Monday, October 04, 2004
 
 
Thanks guys.  Great points.  Couple addendums:

Yes, my (sub)class is really simple.  It is merely those two int's.  That's why I felt weird makig a whole new .java file and .class (and adding junit tests) just for something that simple.

If it is common practice to make a separate .java file for something that simple than I don't mind doing it.  Does accessing  a lot data from a separate .class file hurt performance at all?  I can see how it would based on c++ knowledge.  How I process and use array/vector class should affect performance though.

The size of the array is fixed after init and instantiation.

I will do some reading on the Vector class now.

Thanks again.  I'll be glad to listen to any new points.

Monday, October 04, 2004
 
 
That should be "...I CAN'T see how it would based on c++".  Meaning I think lots of small, separate .class file won't hurt performance.

Monday, October 04, 2004
 
 
In answer to "isn't it wierd to have a class with just two members in a file by itself?"...

This is one of those things you just wouldn't do in java.

If the class has no meaning outside it's parent class, then it's an inner class (or struct); you declare it inside the parent, and it's a moot point.

If it has meaning outside the class, then OO philosophy demands that you consider what it's behavior is. You quickly find that there are a lots of methods you didn't initially consider.

Take a look at Point from JDK for an example of this:

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/Point2D.html
Aaron
Tuesday, October 05, 2004
 
 
I don't mean to say that you'd *never* do this. There are always exceptions.

Come to think of it, Exceptions are actually a good example of where you might end up with ridiculously small source files.

But on the whole, you dont usually have classes with one or two members. They either grow, or get gobbled up by other things.
Aaron
Tuesday, October 05, 2004
 
 
Yes, the attitude of having separate files for everything started with C.  Some C standards require you to have a separate C file for every subroutine. 

Personally I consider this over-kill.  I prefer to 'clump' my C routines, so each 'clump' provides a 'service'.  I have a config file reader/writer C file, for instance, which provides all the calls necessary to define, read, and write a config file for a program.

However, having this legacy, Java took it one step further by the COMPILER requireing a separate file for each class, AND the file name MUST match the class name.  This does mean that some small classes will have very short source files -- but that's not really a problem.

Conclusion:  Yes, that's how Java does it, you're right you may have some very small class files, and that's just the way it works.
AllanL5
Wednesday, October 06, 2004
 
 
Oh, and your example seems to imply an 'inner structure' -- as said earlier, typically this would NOT be declared separately.  The exception to this would be if you were re-using that inner structure someplace else.

If you DID want to re-use that inner structure, then yes you should have a very short file.
AllanL5
Wednesday, October 06, 2004
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz