Anatomy of Java Program
Java Program Anatomy
Introduction to Java Classes and Objects
- The lecture introduces the anatomy of a simple Java program, covering classes, objects, methods, naming conventions, structure, and packages.
- A class is defined as a blueprint for creating objects. An object is an instance of a class.
- Example: A factory producing cars illustrates that the factory represents the class while each car produced is an object.
Class Structure in Java
- To create a class in Java, use the
classkeyword followed by the class name and braces `` to enclose the code block.
- The syntax of defining a class is crucial; it dictates how classes are structured in Java programming.
Understanding Methods
- A method is described as a group of instructions designed to perform specific tasks (e.g., adding two numbers).
- Each method consists of four main parts: return type, method name, parameters (inside parentheses), and body (code block within braces).
Calling Methods
- Methods must be written inside classes; they cannot exist independently.
- To call a method, use its name followed by parentheses containing any required parameters. This action executes the method's code block.
Importance of the Main Method
- The
mainmethod serves as the entry point for execution in every Java program; it must be present for successful execution.
- All code within the
mainmethod will run when executing the program.
Access Modifiers Overview
Access Modifiers and Naming Conventions in Java
Understanding Access Modifiers
- Access modifiers in Java include public, private, protected, and default. These keywords control the visibility of classes and methods within a program.
- The significance of these access modifiers will be elaborated upon later in the Object-Oriented Programming (OOP) section. For now, it's essential to recognize their role as access modifiers.
Naming Conventions in Java
Pascal Case Convention
- The Pascal case convention capitalizes the first letter of each word when naming classes. For example: "ThisIsAName". This is specifically used for class names in Java.
Camel Case Convention
- In camel case convention, the first letter of the first word is lowercase while subsequent words are capitalized (e.g., "thisIsAMethod"). This convention is applied when naming variables and methods in Java.
Snake Case Convention
- Snake case uses underscores between words (e.g., "this_is_a_variable"). While there are other conventions, these three are fundamental to understanding how to name elements in Java programs effectively.
Structure of a Simple Java Program
- Every Java program must contain at least one class; this is foundational to its structure. The class begins with an access modifier such as public followed by the class name (e.g.,
public class Main).
- Inside this class, there exists a main method which serves as the entry point for execution (
public static void main(String[] args)). It includes parameters that allow data input for processing within the method's body.
Key Takeaways from Java Program Structure
- Remember that:
- Each Java program contains at least one class.
- Classes use Pascal case while methods utilize camel case.
- The main method must be named exactly as "main" with all lowercase letters; it cannot be altered or renamed.