Fields
Description
Fields store an object's state.
Explanation
Each object gets its own field values.
Example
String name;
int score;← Chapter 6 — Object-Oriented Programming
mixed
A class is a blueprint that describes data and behavior. An object is a concrete instance created from that blueprint.
For example, a Student class can describe a name and score, while Aditi and Rohan are individual Student objects with their own values.
Fields store an object's state.
Each object gets its own field values.
String name;
int score;new creates an object from a class.
The variable stores a reference to that object.
Student aditi = new Student();Putting related data and behavior together makes programs easier to understand. Start with small classes whose responsibility is obvious.
public class StudentDemo {
static class Student {
String name;
int score;
}
public static void main(String[] args) {
Student aditi = new Student();
aditi.name = "Aditi";
aditi.score = 92;
System.out.println(aditi.name + ": " + aditi.score);
}
}