Source File Structure in java

Welcome to world of Programming ! Whether you are a notice beginner coder, student understanding the fundamental structure of Java source file is crucial. In this article , we will unravel the mysteries behind java source file ,providing you with a solid foundation for writing clean ,efficient ,and maintainable code ,and clear the concept behind java source file and Tips .

In this article we discuss this concept with Question and Answer based .


Q1. In Java Program How many Class can we Take ? 


Ans. In java program we can take any no of classes .and we can compile them but we cant run without Main method 

Demo with code : 

Java code snippet of Multiple class without public access modifier

Code Out Put Snippet

Snippet of of Generated .class file for every class created in Java program



Q2. Which class name do we give for the Java programm? 

Ans : Any Class name we can give for Java program( as like shown in above demo with code) but if we declare any class of them as Public we have to compulsory create a java program file with that class name . 

important  conclusion :  java program contain any no of classes but at most one public class can be declare and more than one public class is not allowed .

Demo with code 1(with public class but name of file is different ) :

It generate following Error: 
Demo.java:6: error: class B is public, should be declared in a file named B.java
public class B {
       ^
1 error


public class declaration but file name is different



Demo with Code 2(with public class and same name of file ) :






Q3. Can we only give that Class name to file Which contain Main method ?

Ans :  No . We can give Any class name to that file , But if there is one class who declared as public and which contain Main method in that condition we have to compulsory file name same as that class name. Other wise without public class we create many classes in one program with many Main method It will be compiled and Executed. 

Demo with Code (number of classes with no of Main methods with Different File name ) :



Conclusion :

This Post contain basic guidance ,tips which should know every student and beginner who is learning java programming.

Comments