It is important to understand the basic structure of a Java program and how java works to build larger applications. We know, Java is an Object-Oriented programming language, thus it is a collection of objects and methods.
A typical Java program will contain, package, class, methods, objects, instant variables, etc. Let us discuss them in detail.
Package
A package is like a directory where we can find related folders. In Java programming, a package contains related classes which help in solving name conflicts and writing maintainable code.
Class
A class in a Java program is a user-defined statement that is further used to create objects. A class can house numerous objects and their properties. The components of a ‘class’ are:
- Modifiers: A class can be public or has default access
- Class keyword: Class keyword is a keyword used to create the class.
- Class name: The class name must begin only with a capital letter.
- Superclass(if any): A subclass can be derived from an existing class, in that case, the latter is called a parent class.
- Body: The class body is surrounded by braces, { }.
Methods:
A class can contain numerous methods. The logic of the program, the actions to be performed, and all other executable commands are written inside the method. The name of a method should start with a Lower Case letter. In the case of multiple words, the first letter of the inner words should be in uppercase.
Objects
Any entity having a state and behavior is termed as an object. In java, an object is created to meet the desired outcome. An object is created from a class. There can be multiple objects in the same class. We can modify an object, move them, call its methods, and combine it with other objects.
Instance Variables
Instance variables (global variables) hold values that can be accessed by other sub-classes, methods, and constructors or blocks.
Basic Syntax:
While writing a code in java programming we need to follow certain steps. They can be termed as basic syntax or syntax of java. They are:
- Case Sensitivity − Java is case sensitive, which means ‘Ab’ and ‘aB’ are different
- Class Names − The first letter in a class must be in Upper Case. If several words are used to form a name of the class, each inner word’s first letter should be in Upper Case. Example: class HelloWorldProgram
- Method Names – The name of a method must start with a Lower Case letter. If several words are used to form the name of the method, then each inner word’s first letter should be in Upper Case. Example: public void myFirstMethod()
- Program File Name − Program File Name – The name of the file and the class name should match precisely (Java is case sensitive). The file should be saved with ‘.java’ extension or else the file will not be compiled.
- In case you do not have a public class present in the file, then the file name can be different than the class name. Public class in a file is not mandatory.
- public static void main(String args[ ]) − Java program processing starts from the main() method which is mandatory for every Java program.
Sample Program 1
import java.io.*; public class Student { String name; int age; String standard; double salary; // This is the constructor of the class Student public Student(String name) { this.name = name; } // Assign the age of the Student to the variable age. public void stdAge(int stdAge) { age = stdAge; } /* Assign the designation to the variable designation.*/ public void stdStandard(String stdStand) { standard = stdStand; } /* Print the Student details */ public void printStudent() { System.out.println("Name:"+ name ); System.out.println("Age:" + age ); System.out.println("Standard:" + standard ); } }
Sample Program 2
import java.io.*; public class StudentTest { public static void main(String args[]) { /* Create two objects using constructor */ Student stdOne = new Student("James Smith"); Student stdTwo = new Student ("Mary"); // Invoking methods for each object created stdOne.stdAge(16); stdOne.stdStandard("Junior"); stdOne.print Student (); stdTwo.stdAge(18); stdTwo.stdStandard("Senior"); stdTwo.printStudent(); } }
Output:
Name:James Smith
Age:16
Standard:Junior
Name:Mary Anne
Age:18
Standard:Senior