• Post Reply Bookmark Topic Watch Topic
  • New Topic

How many classes can be in one .java file?  RSS feed

 
Rusty Neal
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a software system engineer by schooling, training and work experience. I am not a programmer, I work at protocol level not language. I decided to teach myself Java so I would be a better software system engineer, which is how and why I obained the book, "Head First Java".
With that said, I'm going through each example in the book trying to figure out what to type and where. I'm using both the command line technique and NetBeans IDE to compare results. I'm stuck though on what the book was instructing us to do, and I can't get my .java files to compile in either environment.

Here is my question, which wasn't clear in the book. When I write the Dog class, then the DogTestDrive class, are these in the same notepad file or should each class be in a different notepad file? I have been able to get the DogClass file to compile which is just the class example for Dog. But the file that I created separately for DogTestDrive (i.e., DogTestDrive.java) is not compiling. So then I put both sets of code in one file (i.e., the class Dog lines then the class DogTestDrive lines after that), which didn't compile either. So I am missing probably a simple point. I did see in the front of the book where one class in one file. So does that mean that one and only one class goes into a file?
Thanks in advance,
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you're a beginner, the short answer is "Yes, always put each Java class's code into a separate file". (That isn't a firm rule, but at this point the exceptions are just going to confuse you.)
 
Rusty Neal
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Actually, the more I stared at the rusults I was getting in the NetBean IDE, I came to the conclusion that they both did compile and run. I'm moving on to the second problem in the book to see if my theory holds. I am following your advice with one class, one file. Again, thanks.
Rusty
 
Greg Brannon
Bartender
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There can only be one public class per *.java file, but I've never seen a limit for the number of total classes in a java file. Some sage opinions have said one class per file is good programming practice, but that doesn't seem possible on more advanced, especially GUI, projects.

There is probably some dependency of your second class on the other so that it is not able to compile on its own, but I'm guessing. You'll have to show me the code to get a better answer.
 
Rusty Neal
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg, thanks for the reply. I figured out what I was doing wrong and it now works. I'm working on the second problem in the book to see if my new found information works. (I'm sure you know it will, I just have to do it to learn.)
Regards,
Rusty
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Brannon wrote:There can only be one public class per *.java file.


There can only be one public top-level class per *.java file... but here we are getting into deep waters for the newbie. Now we have to explain what a "top-level" class is, and why you might want a class which isn't public; that's why I short-cut this discussion and just provided a simple version of the rule for newbies.

As for the "How many classes" question: the designers of Java almost always followed the "Zero, One, Infinity" rule when designing Java constructs. That rule states that questions of the form "How many X's can I use in this situation" must be answered by one of the three values zero, one, and infinity. There shouldn't be a rule which says you can have no more than 63 parameters for a method, for example. And I think you'll find that they have only deviated from that rule in situations where hardware is dictating a limitation.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always learn something here. Thanks for taking the time to elaborate.
 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rusty Neal wrote:I'm a software system engineer by schooling, training and work experience. I am not a programmer, I work at protocol level not language. I decided to teach myself Java so I would be a better software system engineer, which is how and why I obained the book, "Head First Java".
With that said, I'm going through each example in the book trying to figure out what to type and where. I'm using both the command line technique and NetBeans IDE to compare results. I'm stuck though on what the book was instructing us to do, and I can't get my .java files to compile in either environment.

Here is my question, which wasn't clear in the book. When I write the Dog class, then the DogTestDrive class, are these in the same notepad file or should each class be in a different notepad file? I have been able to get the DogClass file to compile which is just the class example for Dog. But the file that I created separately for DogTestDrive (i.e., DogTestDrive.java) is not compiling. So then I put both sets of code in one file (i.e., the class Dog lines then the class DogTestDrive lines after that), which didn't compile either. So I am missing probably a simple point. I did see in the front of the book where one class in one file. So does that mean that one and only one class goes into a file?
Thanks in advance,



It's not about how many classes but rather the size of the java file. I have created one program for that. I have created a java file with a number of classes and try to compile it. For a certain number which exceeds the limit for creating the byte array will result in compilation failure. Let me elaborate.





In this code I have changed the value of i from 1000 to increasing one zero every time and I try to compile the JavaTest.java file. For some large values of i I got an error as



It indicates that the class java.nio.HeapByteBuffer is throwing error because it's super class is throwing the error at creating a byte array as



Here the cap variable represents the size of the java file in bytes. Now this limit is not fixed and depends on the runtime environment that is the free space available. The value of cap is around 87972405 (in my machine it is showing) for which I have written another program to test it.



However the value of i varies even in a particular machine. You can check at your machine for your interest. For more precise value of i you can implement a Thread which will after regular intervals say 5 secs will invoke the garbage collector to free memory while the main thread iterates over the loop.
 
Rusty Neal
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kumarjit,
Thank you very much for a cool explanation and sample application. I will go through your code so I can digest what you wrote and how it works. This will take me a little while because I'm a beginner. But this is the BEST way to learn, looking at other people's code. Again, thanks for taking the time to give a great example to play with.
Regards,
Rusty
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!