Java Classes:
In Java, a class serves as a blueprint or a template for creating objects. It is a fundamental concept in object-oriented programming (OOP) and defines the structure and behavior that objects of that class will possess.
A class is a template to create objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects.
Properties of Java Classes:
- Class is not a real-world entity. It is just a template or blueprint, or a prototype from which objects are created.
- A class itself does not occupy memory for its attributes and methods until an object is instantiated.
- A Class in Java can contain Data members, methods, a Constructor, Nested classes, and interfaces.
Java Objects:
Objects are the instances of a class that are created to use the attributes and methods of a class. A typical Java program creates many objects, which as you know, interact by invoking methods. An object consists of:
State: It is represented by attributes of an object. It also reflects the properties of an object.
Behavior: It is represented by the methods of an object. It also reflects the response of an object with other objects.
Identity: It gives a unique name to an object and enables one object to interact with other objects.
Java Variables:
In Java, variables are containers used to store data in memory. Variables define how data is stored, accessed, and manipulated.
A variable in Java has three components,
Data Type: Defines the kind of data stored (e.g., int, String, float).
Variable Name: A unique identifier following Java naming rules.
Value: The actual data assigned to the variable.
Rules to Name Java Variables:
- Start with a Letter, $, or _ – Variable names must begin with a letter (a–z, A–Z), dollar sign $, or underscore _.
- No Keywords: Reserved Java keywords (e.g., int, class, if) cannot be used as variable names.
- Case Sensitive: age and Age are treated as different variables.
- Use Letters, Digits, $, or _ : After the first character, you can use letters, digits (0–9), $, or _.
- Meaningful Names: Choose descriptive names that reflect the purpose of the variable (e.g., studentName instead of s).
- No Spaces: Variable names cannot contain spaces.
- Follow Naming Conventions: Typically, use camelCase for variable names in Java (e.g., totalMarks).
Top comments (0)