Java Program Structure
Java Program Structure
Description
features of class. This program is written on few lines, and its only task is to print
“Hello World from Java” on the screen. Refer the following picture.
1. “package sct”:
which classes are stored. Package is used to organize the classes based on
functionality. If you omit the package statement, the class names are put into the
default package, which has no name. Package statement cannot appear anywhere in
program. It must be first line of your program or you can omit it.
2. “public class HelloWorld”:
a. public: This is access modifier keyword which tells compiler access to class.
value).
b. class: This keyword used to declare class. Name of class (HelloWorld) followed
by this keyword.
3. Comments section:
a. Line comments: It start with two forward slashes (//) and continue to the end of
b. Block comments start with a forward slash and an asterisk (/*) and end with an
asterisk and a forward slash (*/).Block comments can also extend across as many
lines as needed.
c. void: This keyword declares nothing would be returned from method. Method
c. println:It is utility method name which is used to send any String to console.
d. “Hello World from Java”:It is String literal set as argument to println method.
represent objects from the real world. Each Java program has at least one class
that knows how to do certain things or how to represent some type of object.
For example, the simplest class, HelloWorld,knows how to greet the world.
Classes in Java may have methods (or functions) and fields (or attributes or
properties).
Let’s take example of Car object which has various properties like color, max
speed etc. along with it has functions like run and stop. In Java world we will
view plainprint?
1. package sct;
7. }
12. }
13. }
Lets make a class named CarTestwhich will instantiate the car class object and
1. package sct;
7. System.out.println(maruti.carInfo());
8. System.out.println(ferrari.carInfo());
9. }
10. }
Output of above CarTest java class is as below. We can run CarTest java program
because it has main method. Main method is starting point for any java program
execution. Running a program means telling the Java VIrtual Machine (JVM) to
"Load theclass, then start executing its main () method. Keep running 'til all
Java identifiers must start with with a letter, a currency character ($), or a
int 4var =10 ; // this is invalid, identifier can’t start with digit.
Identifiers, method names, class names are case-sensitive; var and Varare two
different identifiers.
You can’t use Java keywords as identifiers, Below table shows list of Java
keywords.
assert enum
Classes and interfaces: The first letter should be capitalized, and if several
words are linked together to form the name, the first letter of the inner words
Methods: The first letter should be lowercase, and then normal camelCaserules
getBalance
doCalculation
setCustomerName
Variables: Like methods, the camelCase format should be used, starting witha
lowercase letter. Sun recommends short, meaningful names, which sounds good
buttonWidth
accountBalance
empName
Constants: Java constants are created by marking variables static and final.
separators:
MIN_HEIGHT
There can be only one public class per source code file.
Comments can appear at the beginning or end of any line in the source code
file; they are independent of any of the positioning rules discussed here.
If there is a public class in a file, the name of the file must match the nameof
the public class. For example, a class declared as “public class Dog { }” must
Source code of sample discussed attached here to run directly on your system.
Summary
Java program has first statement as package statement (if package declared).
One Java file can have multiple classes declared in it but only one public class
Java class can have instance variable such as methods, instance variable.
Source: http://www.w3resource.com/java-tutorial/java-program-structure.php