0% found this document useful (0 votes)
1K views

Java Source File Structure

This document discusses the structure of Java source files. It covers: 1. A Java source file can contain multiple classes, but only one class can be public. If a class is public, the source file name must match the class name. 2. When compiling a Java source file, a .class file is generated for each class. These .class files allow the classes to be executed. 3. Import statements are used to import classes from other packages to avoid fully qualified names and improve readability of the code. There are explicit and implicit import statements.

Uploaded by

Santhosh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Java Source File Structure

This document discusses the structure of Java source files. It covers: 1. A Java source file can contain multiple classes, but only one class can be public. If a class is public, the source file name must match the class name. 2. When compiling a Java source file, a .class file is generated for each class. These .class files allow the classes to be executed. 3. Import statements are used to import classes from other packages to avoid fully qualified names and improve readability of the code. There are explicit and implicit import statements.

Uploaded by

Santhosh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

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
{
}

If class Two is public then name of the program should be


Two.java.
Otherwise, we will get compile time error saying,
Class Two is public, should be declared in a file named Two.java
• Case 3:
• Example:
Class One
{
}
public Class Two
{
}
public Class C
{
}

If class Two and C is declared as public and name of the program is


Two.java then we will get compile time error saying,

Class C is public, should be declared in a file named C.java


• Whenever we are compiling a java program for every class
present in the program a separate .class file will be generated.
Example:
class A
{
public static void main(String[] args)
{
System.out.println(“A class main”);
}
}
class B
{
public static void main(String[] args)
{
System.out.println(“B class main”);
}
}
class C
{
}

javac One.java

A.class B.class C.class One.java


1) Whenever we are executing a java class, the corresponding
class main method will be executed.
2) If the class doesn’t contain main method then we will get
runtime exception saying,
noSuchMethodError:main
3) If the corresponding .class file not available then we will get
runtime exception saying
noClassDefFoundError

java A --- java B ---


output: A class main output: B class main
java C --- java One --
RE :noSuchMethodError:main RE:noClassDefFoundError
Import statement:
class One
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
}
}

CE: cannot find symbol


symbol: class Scanner
location: class One
We can solve this problem by using fully-qualified name

java.util.Scanner s=new Scanner(System.in);


• The problem with usage of fully-qualified name everytime, it
increases the length of the code and reduces readability.
• We can solve this problem by using import statement.
• Whenever we are writing import statement, it is not required
to use fully-qualified name everytime. We can use short
name directly.
Example:
import java.util.Scanner;
class one
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
}
}
There are two types of import statements

 Explicit class import


Eg: import java.util.Scanner;
 Implicit class import
Eg: import java.util.*;
Case 1:
import java.util.*;
import java.util.*;
class Test
{
public static void main(String[] args)
{
Date d=new Date();
}
}

CE: reference to Date is ambiguous

Because both the package(util and sql) contain Date class.


while resolving class name compiler will always gives
precedence in the following order.
» Explicit class import
» Classes present in current working directory
» Implicit class import
Example:
import java.util.Date;
import java.sql.*;
class Test
{
public static void main(String[] args)
{
Date d=new Date();
System.out.println(d.getClass().getName());
}
}

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 ---

directory not found: Z:


At the time of execution we have to use fully-qualified name

java com.cricket.Test ---


Output:
package demo
Conclusion:
1) In any java source file, there can be at most one package statement. That is
more than one package is not allowed. Otherwise, we will get compile time error.
Example:

package pack1;
package pack2;
public class A
{
}

CE: class,interface or enum expected


2) If any java program, the first non-comment statement
should be package statement( if it is available). Otherwise,
we will get compile time error.
Example:

import java.util.*;
package pack;
public class A
{
}

CE: class,interface or enum expected


The following is valid java source file structure.

package statement;
import statements;
class / interface / enum declarations
Thank you

You might also like