Java Source File Structure
Java Source File Structure
by
S.Santhosh
1721041
• JAVA SOURCE FILE STRUCTURE:
1. A java program can contain any number of classes.
2. At most one class can be declared as public.
3. If there is a public class then name of the program and
name of the public class must be matched. Otherwise, we
will get compile time error.
Example:
class One
{
}
class Two
{
}
class C
{
}
• Case 1:
Example:
Class One
{
}
Class Two
{
}
Class C
{
}
– If there is no public class then we can use any name and there
is no restrictions.
• One.java
• Two.java
• C.java
• Santhosh.java
• Case 2:
• Example:
Class One
{
}
public Class Two
{
}
Class C
{
}
javac One.java
Output:
java.util.Date
• Packages:
• It is an encapsulation mechanisms group related classes and
interfaces into a single unit, which is nothing but package.
• Example:
– All classes and interfaces which are required for database
operation are grouped into a single package which is
nothing but java.sql package
The main advantages of package are:-
1. To resolve naming conflict( that is unique identification
of our components).
2. It improves maintainability of the application.
3. It proves security for our components.
Example:
Package com.cricket;
Public class Test
{
public static void main(String[] args)
{
System.out.println(“package demo”);
}
}
Compile – 2 ways:
javac Test.java ---
Generated .class file will be placed in current working
directory.
javac –d . Test.java ---
Generated .class file will be placed in corresponding package
structure.
As destination instead of . We can take any valid directory
name.
javac – d F: Test.java ---
If the corresponding package structure not already
available then this command itself will create
corresponding package structure.
If the specified directory not available then
We will get compile time error.
Example:
javac – d Z: Test.java ---
package pack1;
package pack2;
public class A
{
}
import java.util.*;
package pack;
public class A
{
}
package statement;
import statements;
class / interface / enum declarations
Thank you