• Post Reply Bookmark Topic Watch Topic
  • New Topic

Address Book in Java (beginner)  RSS feed

 
Aqsa Anum
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not. 



Save it as TestClass.java.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better question is should you put them into one file.

What do you hope to gain by doing this?
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just an example, I referred to.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Apart from trying to put three classes in one file, are you getting everything else to work?
 
Aqsa Anum
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:Why not. 



Save it as TestClass.java.



Thanks
 
Aqsa Anum
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aqsa Anum
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aqsa Anum
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
[Thumbnail for Capture.PNG]
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are each of the classes outside of the other classes?
 
Aqsa Anum
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Aqsa Anum
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aqsa Anum
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aqsa Anum
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!