Google+

188. Creating more than one Class in a single Java Class file







Till now we have created separate Java Class files for each and every Class in our Java Projects. But we can have more than one Class in a single Java Class file with the below few restrictions -
  1. Only one Class need to be Public 
  2. Public Class name and Java Class file name should be same
  3. main( ) method shouldn't be there in the non-public classes.
Hoping that you have understood the above three restrictions, lets create a  single .java class file and write more than one classes into it.

Lets implement this on Eclipse IDE -

1.Launch Eclipse IDE, Create a Java Project 'Project 22' as shown below -




2. Create a  Java Class file 'ClassOne'  as shown below -



3. Create another Public Class 'ClassTwo' in the same file i.e. 'ClassOne.java' as shown below -



4. View the error displayed in the above step as shown below -



This error is displayed as the Java don't accept more than one public Class in a single .java Class file, i.e. Only one Class can be public when more than one Classes are there in a single Java Class file.

5.  Remove the public access specifier from the Class 'ClassTwo' and observe that the error got resolved as shown below -



6. Create main( ) method in Class 'ClassTwo'  as shown below -



7. Run the .java Class file 'ClassOne.java' as shown below and observe that 'Run As' -> 'Java Application' option is not available as shown below -



'Run As' -> 'Java Application' . is not available as the Class name having main( ) method i.e. ClassTwo is not same as the .java Class file name 'ClassOne.java' that we are trying to Run. Hence 'Run As' -> 'Java Application' is not available. To resolve this problem move the main( ) method from 'ClassTwo' to 'ClassOne' by following the below steps.

8. Move the main( ) method from 'ClassTwo' to 'ClassOne' as shown below -



9. Now Right Click on  the .java Class file 'ClassOne.java' and observe that 'Run As' -> 'Java Application' option is available -


Java sets a rule that the .java Class file name 'ClassOne.java' should be same as the Class name which is having the main( ) method i.e. 'ClassOne' . Hence 'Run As' -> 'Java Application' is available as shown below. If not you wont get any compile error, but you wont be able to use the 'Run As' -> 'Java Application'

10. In 'ClassTwo' , create a method 'two' with print statement as shown below -


11. As we cannot access the instance variables and methods in the 'ClassTwo' from 'ClassOne' , we've to create object for 'ClassTwo' in 'ClassOne' as shown below -


12. Now access and call the method two( ) in ClassTwo using the created object as shown below -


13. Run the .java Class file 'ClassOne.java' and observe that print statements in both the Classes got printed in the output as shown below -








Please comment below to feedback or ask questions.

Objects cant be created for abstract Classes  will be explained in the next post.






No comments: