Professional Documents
Culture Documents
Lec3-Anatomy of A Java Program
Lec3-Anatomy of A Java Program
Program
Lecture 3
Based on Slides of Dr. Norazah Yusof
1
Creating, Compiling, and
Running Programs
Create/Modify Source Code
Result
2
Parts of a Java Program
• A Java source code file contains one or more Java
classes.
• If more than one class is in a source code file, only
one of them may be public.
• The public class and the filename of the source
code file must match.
ex: A class named HelloApp must be in a file named
HelloApp.java
• Each Java class can be separated into parts.
3
Parts of a Java Program
• Example: HelloApp.java
• To compile the example:
• javac HelloApp.java
• Notice the .java file extension is needed.
• This will result in a file named HelloApp.class being
created.
• To run the example:
• java HelloApp
• Notice there is no file extension here.
• The java command assumes the extension is .class.
4
Analyzing the Example
This is a Java comment. It is
ignored by the compiler.
// This is my first Java program.
This is the class header
public class HelloApp for the class HelloApp
{
5
Analyzing the Example
6
Analyzing the Example
7
Anatomy of a Java Program
• Comments
• Keywords
• Modifiers
• Statements
• Blocks
• Classes
• Methods
• The main method
• Package
8
Comments
• Help the programmers to communicate and understand
the program.
• Not a programming statement, thus ignored by the
compiler.
• Preceded with // on a line
• Enclosed between /* and */ on one or several lines.
9
Comments on several lines
10
Key Words
• Words that have a specific meaning to the compiler
• Key words in the sample program are:
13
• Example:
public class ClassA {
public static void main (String[] args) {
System.out.println ("Try your best");
}
}
14
Statements
• represents an action or a sequence of actions.
• Example of statement:
System.out.println("Welcome to Java!")
is a statement to display the greeting
"Welcome to Java!"
• Every statement in Java ends with a semicolon ( ; ).
15
Blocks
Groups the components of the program using the
braces { and } in the program.
Every class has a class block that groups the data and the
methods of the class.
Every method has a method block that groups the data and
the methods of the class.
Block may be nested, meaning that one block can be placed
within another.
public class Test {
Class block
public static void main(String[] args) {
System.out.println("Welcome to Java!"); Method block
}
}
16
Classes
• class is the essential Java construct.
• Classes are central to Java
• Programming in Java consists of defining a number of classes:
17
Classes
• Example 1:
public class ClassA {
public static void main (String[ ] args) {
System.out.println ("Try your best");
}
}
18
Classes
• Example 2: Program named ClassA.java below has two
classes i.e. ClassA and ClassB.
public class ClassA {
private int yearborn=1988;
public String methodA() { return "Aim High"; }
public int getYearBorn() { return yearborn; }
}
class ClassB {
public static void main (String[] args) {
ClassA obj1 = new ClassA ( );
System.out.println (“ Your age: “ + (2021 - obj1.getYearBorn()));
System.out.println (“My message: “ + obj1.methodA());
}
}
19
Methods
• A collection of statements that performs a sequence
of operations.
• Contained in a class.
• If a method is intended to be used to communicate with
or pass information to an object, it should be declared
public.
• Example: Method println() is an instance method that
belongs to an object instance and is applied to an object
(System.out).
20
Methods
methodA
getYearBorn is aisclass
a class method
method in in
ClassAClassA . public
. public modifier
modifier indicates
indicates it it
• Example: can becan be accessed
accessed from anywhere.
from anywhere.
String indicates
int indicates it return it returnofa value of
a value
public class ClassA { String.
private int yearborn=1988; type integer.
public String methodA() { return "Aim High"; }
public int getYearBorn() { return yearborn; }
}
main method is method
getYearBorn in ClassB is.invoked
methodA method
from is invoked
instance fromobj1.
of the class,
class ClassB {
instance of the class, obj1.
public static void main (String[] args) {
ClassA obj1 = new ClassA(); create an instance of a class.
System.out.println (“Your age: “ + (2021 - obj1.getYearBorn()));
System.out.println (“My message: “ + obj1.methodA());
}
}
21
main Method
public class ClassName
{
public static void main(String[] args) {
// Statements;
}
} KeywordThisvoid
is theindicates
parameter ofdata
the the main
type
The method in Java
This method is public, i.e. visible from
main is always
method.
returned from It takes
this arguments
method is of an
nothing or array
meaning that
anywhere that can see this class.
static, this method can
of Strings. The data type String starts
no value.
be run without creating an instance of
with an upper case S. The square
the class.
brackets indicate an array. 22
Command-Line Arguments
24
import
• Full library class names include the package name.
For example:
java.awt.Color
javax.swing.JButton
• import statements at the top of the source file let
you refer to library classes by their short names:
import javax.swing.JButton;
...
JButton go = new JButton("Go"); Fully-qualified
name
25
import
26
Package
• Java is a package-centric language; for good
organization and name scoping, put all classes into
packages.
• A class with default access can be seen only by
classes within the same package.
27
Package
• Example: class Sludge and class Goo are both in different
packages.
package cert;
public class Sludge {
public void testIt() {
System.out.println("sludge");
}
}
package book;
import cert.*; // Import all classes in the cert
package class Goo {
public static void main(String[] args) {
Sludge o = new Sludge();
o.testIt();
}
}
28
Source File Declaration Rules
• A source code file can have only one public class.
• If the source file contains a public class, the filename
must match the public class name.
• A file can have only one package statement, but
multiple imports.
• The package statement (if any) must be the first (non-
comment) line in a source file.
• The import statements (if any) must come after the
package and before the class declaration.
29
Source File Declaration Rules (cont.)
• If there is no package statement, import statements
must be the first (non-comment) statements in the
source file
• package and import statements apply to all classes in
the file.
• A file can have more than one nonpublic class.
• Files with no public classes can have a name that does
not match any of the classes in the file.
30