Header Ads

Java Source File and Java Source File structure

Java Source File

Java Source File_img



1.A Java source file is a plain text file containing Java source code and having .java extension.

2.The ".java" extension means that the file is the Java source file. 

3.Java source code file contains source code for a class, interface, enumeration, or annotation type. 

4.There are some rules associated to Java source file. We should abide to following rules while writing Java source code.


1.Rule says that there should be only one public class per source code file.

2.Comments can be anywhere at the beginning or end of any line in the source code file; they are independent of any of the positioning rules discussed here. Java comment can be inserted anywhere in a program code where a white space can be present.

3.These rule should be carefully taken into mind which says that if there is a public class in a file, the name of the file must match the name of the public class. For example, a class declared as public class Game { } must be in a source code file named Game.java.

4.These rule is related to the package statement which says that if the class is part of a package, the package statement must be the first line in the source code file, before any import statements  present in the file.

5. Import statements are always between the package statement (if there is one) and the class declaration and  if there is not a package statement, then the import statement(s) must be the starting line(s) in the source code file. Also analyse that  if there are no package or import statements , the class declaration must be the first line in the source code file.

6.package and import statements apply to all classes within a source code file. In other words, there is no way to declare multiple classes in a file and which can be in different packages, or use different imports.

7.A file can have more than one non~public class.

Also files which has non~public classes can have a name that does not match any of the classes in the file.



Java Source File Structure



A java Program can contain any number of Classes but there should be at most one class which can be declared as public."If there is a public class the name of the Program and name of the public class must be matched otherwise we can get compile time error".

If there is no public class then any name we give for java source file.

Example:



                                       class One


                                       {



                                       }


              


                                       public class Two


                                       {



                                       }



                                     public class Three


                                       {



                                       }                        



Case 1:

If in a java program,there is no public class then we can use any name for java source file there are no restrictions.

Example: 

         Bus.java 

         Car.java

         Train.java 

         Alien.java



Case 2:

If class Car declared as public then the name of the Program should be Car.java otherwise we will get

compiler time error saying "class Car is public,should be declared in a file named Car.java".


Case 3:

1.If both Car and Train classes are declared as public and name of the file is Car.java then we will get compile

time error saying that class Train is public , should be declared in a file named Train.java


2.It is highly recommended to take only one class for source file and name of the Program/file must be same as class name. This approach improves readability and understandability of the code.


Example: 

//Filename: Alien.java


class Bus

{

  public static void main(String args[]){

   System.out.println("Bus class main method is executed");

}

}



class Car

{

  public static void main(String args[]){

   System.out.println("Car class main method is executed");

 }

}


class Train

{

  public static void main(String args[]){

   System.out.println("Train class main method is executed");

 }

}



class L

{



}


Result:

1. javac Alien.java (execute these command on cmd)

   java Bus   (execute these command after "javac Alien.java" on cmd)

   Output: Bus class main method is executed


2. javac Alien.java (execute these command on cmd)

   java Car   (execute these command after "javac Alien.java" on cmd)

   Output: Car class main method is executed


3. javac Alien.java (execute these command on cmd)

   java Train   (execute these command after "javac Alien.java" on cmd)

   Output: Train class main method is executed


4. javac Alien.java (execute these command on cmd)

   java L   (execute these command after "javac Alien.java" on cmd)

   Output: Exception in thread "main" java.lang.NoSuchMethodError: main


5. javac Alien.java (execute these command on cmd)

   java K   (execute these command after "javac Alien.java" on cmd)

   Output: Exception in thread "main" java.lang.NoClassDefFoundError: K   


1. We can compile a java Program but not java class in that Program for every class one dot class file will be created.

2. We can run a java class but not java source file whenever we are trying to run a class the corresponding class main method will be executed.

3. If the class won't contain main method then we will get runtime exception saying "NoSuchMethodError: main".

4. If we are trying to execute a java class and if the corresponding .class file is not available then we will get runtime execution saying "NoClassDefFoundError: K".   


An Inspirational quote: 

"A winner is a dreamer who never gives up."
 


Steps to Subscribe my blog in 15 secs:
1. Enter your mail id in "Subscribe" box.
2. Enter Captcha for verification.
3. A mail will be received with link in your mail id.Just Click on link  and  then Done .Thank you for subscribing :)



Happy Reading.Happy Learning!!! See you in next post  :)

No comments

Note: Only a member of this blog may post a comment.