Forums Register Login

Address Book in Java (beginner)

+Pie Number of slices to send: Send
Hey everyone!
I made Address Book in Java, for this I have had to make three notepad files for three different classes. Can I put these three classes in one file and if yes, How can I ?

First File:




Second File:




Third File:


+Pie Number of slices to send: Send
Why not.  



Save it as TestClass.java.
+Pie Number of slices to send: Send
A better question is should you put them into one file.

What do you hope to gain by doing this?
+Pie Number of slices to send: Send
 

Aqsa Anum wrote:Hey everyone!
I made Address Book in Java, for this I have had to make three notepad files for three different classes. Can I put these three classes in one file and if yes, How can I ?


Yes, you can combine all that code into one file. They will have to be combined in the first file, which is the one that has the public class in it.  The rule in Java is that you can have only one top-level public class in a file and that public class must have the same name as the file. So, you would edit your PersonInfo.java file and either copy/paste in the code that you have in the second and third files or you can type all the code in again manually. Another alternative would be to go to your OS command line and concatenate the second and third files to the first file. Obviously, you'd also need edit PersonInfo.java further to resolve errors about import statements.
1
+Pie Number of slices to send: Send
 

Swastik Dey wrote:Why not.  



Save it as TestClass.java.


Conversely, why would you want to do that? I can't imagine why a test class would need more visibility than the classes that it tests. Why would you give more prominence/importance to the test class over the "production" classes?
+Pie Number of slices to send: Send
It's just an example, I referred to.
1
+Pie Number of slices to send: Send
Welcome to the Ranch

Apart from trying to put three classes in one file, are you getting everything else to work?
+Pie Number of slices to send: Send
 

Swastik Dey wrote:Why not.  



Save it as TestClass.java.




Thanks
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:Welcome to the Ranch

Apart from trying to put three classes in one file, are you getting everything else to work?



Yup I am getting everything else to work
+Pie Number of slices to send: Send
 

Aqsa Anum wrote:

Swastik Dey wrote:Why not.  

Save it as TestClass.java.


Thanks


You should note that there are good reasons to NOT do it this way.
+Pie Number of slices to send: Send
 

Junilu Lacar wrote:

Aqsa Anum wrote:

Swastik Dey wrote:Why not.  

Save it as TestClass.java.


Thanks


You should note that there are good reasons to NOT do it this way.



Please tell me, what are these good reasons ?
Thank you
+Pie Number of slices to send: Send
 

Aqsa Anum wrote:

Swastik Dey wrote:Why not.  



Save it as TestClass.java.




Thanks




Here I am getting one error, when I did this.
Capture.PNG
[Capture.PNG]
1
+Pie Number of slices to send: Send
 

Aqsa Anum wrote:
Please tell me, what are these good reasons ?
Thank you


By making the TestClass as public and the others non-public, you are implying that TestClass is more important than the others. Why would a class that is used for testing be more important than the classes that are being tested?
+Pie Number of slices to send: Send
Are each of the classes outside of the other classes?
+Pie Number of slices to send: Send
 

Junilu Lacar wrote:

Aqsa Anum wrote:
Please tell me, what are these good reasons ?
Thank you


By making the TestClass as public and the others non-public, you are implying that TestClass is more important than the others. Why would a class that is used for testing be more important than the classes that are being tested?



You said right, there are two other classes too, which are more important than testing class. Then Different notepad files for each class looks fine.
But if we want to do it in NetBeans, How we will code it ? Would there also be distinct files for each class ?
+Pie Number of slices to send: Send
 

Norm Radder wrote:Are each of the classes outside of the other classes?



At that time these were not, but now I separated each class and it is working fine
+Pie Number of slices to send: Send
 

Junilu Lacar wrote:

Aqsa Anum wrote:
Please tell me, what are these good reasons ?
Thank you


By making the TestClass as public and the others non-public, you are implying that TestClass is more important than the others. Why would a class that is used for testing be more important than the classes that are being tested?



But I think we can do in this way
+Pie Number of slices to send: Send
 

Aqsa Anum wrote:

Junilu Lacar wrote:

Aqsa Anum wrote:
Please tell me, what are these good reasons ?
Thank you


By making the TestClass as public and the others non-public, you are implying that TestClass is more important than the others. Why would a class that is used for testing be more important than the classes that are being tested?



But I think we can do in this way




Sorry Mistake

+Pie Number of slices to send: Send
The order in which the classes appear in the source file makes no difference. The fact that one is public and the others are not makes the public one appear to be more important than the others.
This one time, at bandcamp, I had relations with a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 8230 times.
Similar Threads
Please help me to solve this error
ArrayList - Contact Information/Address Book
problem in servlet to jsp
Exception in thread "main" java.lang.NullPointerException
Super Class method
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:52:54.