Introduction
Java is a powerful, versatile programming language widely used for building web applications, mobile apps, and enterprise systems. When we start programming in any language, we need to follow a standard structure of writing the program. To understand the structure of a Java program is crucial for beginners. In this article, we will discuss about structure of java program.
1. Basic Components of a Java Program
Java Class
In Java, class is a blueprint for creating objects that share common characteristics/ behaviour and common properties/ attributes. It is a user-defined blueprint or prototype from which objects are instantiated.
For example: in the real world, a specific name of the student (vivek) is an object of Students class.
Example:
public class MyClass {
// Fields, methods, and other components will go here
}
In above example, MyClass is a simple class declaration. The public keyword used which makes this class accessible from anywhere in the application.
Main Method
In your application there should be a main method, which will be the entry point of the application, Java Virtual Machine (JVM) invokes the main method to start the execution of the application.
Example:
public static void main(String[] args) {
// Code to be executed
}
In the above example, public means this method is accessible from anywhere in the application, static keyword is used to access this method without initialising the object of the class, this method can be called along with class name, and void means it will not return any value from this method. String[] args is defined to accept command-line arguments.
2. Structure of a Java Class
Class Declaration
In Java or any other programming language, a class is declared using the class keyword, followed by class name. The class name should start with an uppercase letter by convention.
Syntax of a Class Declaration
Here is a basic example of a class declaration:
public class ClassName {
// Fields (attributes)
private int age;
// Constructor
public ClassName(int initialAge) {
age = initialAge;
}
// Method
public void displayAge() {
System.out.println("Age: " + age);
}
}
Explanation
- public: This is an access modifier which will allow to access this class from any other classes.
- class: This keyword Indicates that you are declaring a class.
- ClassName: The name of the class, you can provide any name as per requirement.
- { }: Encloses the body of the class where fields, methods, and constructors are defined.
The class ClassName in this example has:
- A private field age.
- A constructor that initializes age.
- A method displayAge() that prints the value of age.
Fields/Attributes
Fields (or attributes) are variables defined within a class. They represent the state or characteristics of the object created from the class.
Example:
private int age;
In this example, age is a field of type int. The private modifier restricts access to this field to within the class itself.
Methods
Methods define the behaviors or actions that an object of the class can perform. They can operate on the fields and provide functionality.
Example:
public void displayInfo() {
System.out.println("Age: " + age);
}
This method prints the value of the age field to the console.
Constructors
Constructors are special methods used to initialize objects. They have the same name as the class and do not have a return type.
Example:
public ClassName(int initialAge) {
age = initialAge;
}
In above example, the constructor initializes the age field with a value provided when an object is created.
Access Modifiers
Access modifiers control the visibility of classes, fields, methods, and constructors. The most common modifiers are public, private, protected, and default (no modifier).
- public: This modifier will allow to access the class from any other class.
- private: This modifier will allow to access only within the class.
- protected: This modifier will allow to access within the package and by subclasses.
- Default: This modifier will allow to access only within the package.
Example:
public int publicField;
private int privateField;
3. Basic Syntax Rules
Semicolons
In Java, each statement ends with a semicolon (;). It is used to signify the end of a complete statement or declaration.
Example:
int number = 10;
Braces
Braces ({}) are used to define blocks of code, such as the body of a class, method, or loop. They help in grouping related statements together.
Example:
public class Example {
public void method() {
// Code block
}
}
Comments
Comments are used to add explanations or notes in the code, which are ignored by the compiler.
- Single-line comment: Starts with //.
// This is a single-line comment
- Multi-line comment: Enclosed between /* and */.
/*
This is a multi-line comment
*/
- Javadoc comment: Used for documentation, starts with /** and ends with */.
/**
* This method prints a message.
*/
3. Packages and Import Statements
Packages
Packages are used to group related classes and interfaces, providing modularity and namespace management.
Example:
package com.example;
This line declares that the class belongs to the com.example package.
Import Statements
Import statements are used to include external classes or packages into the current file, allowing you to use their functionality without having to fully qualify their names.
Example:
import java.util.ArrayList;
This line imports the ArrayList class from the java.util package.
5. Example Java Program
Here’s a simple Java program that demonstrates the basic structure:
package com.example;
public class HelloWorld {
// Main method
public static void main(String[] args) {
// Print statement
System.out.println("Hello, World!");
}
}
Breakdown:
- package com.example; specifies the package name.
- public class HelloWorld { } defines the class.
- public static void main(String[] args) { } is the main method where execution starts.
- System.out.println(“Hello, World!”); prints the message to the console.
6. Common Errors and Troubleshooting
Syntax Errors
Syntax errors is mistake in the syntax of a sequence of characters written in java.
Example:
public class Example {
public static void main(String[] args) {
System.out.println("Missing closing brace");
// Error: Missing }
}
Compilation Issues
Compilation issues can arise from incorrect classpath settings or missing files. Ensure that all required files are present and the classpath is correctly set.
Additional Resources