When writing a program in any programming language, it's important to follow a basic structure recommended by experts. Typically, a Java program is made up of the following parts, as shown in the figure below.
This structure if explained below :-
package packageName;
package com.example.myapp;
import packageName.ClassName; // Imports a specific class
import packageName.*; // Imports all classes in the package
import java.util.Scanner; // Imports Scanner class
import java.util.*; // Imports all classes in java.util package
class ClassName {
// Class body
}
public class Car {
// Class members go here
}
dataType variableName = value; // Field or local variable declaration
String model = "Tata Nexon" // Field
int year = 2020; // Field
ClassName() {
// Constructor body
}
public Car() {
System.out.println("Constructor called!");
}
accessModifier returnType methodName(parameters) {
// Method body
}
public void start() {
System.out.println("Car Started");
}
public static void main(String[] args) {
// Code to execute
}
public static void main(String[] args) {
System.out.println("Hello Deepak...!!");
}
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.