Glossary
Behavior
The behavior of an object refers to the actions it can perform, which are defined by its methods. These methods can modify the object's state or interact with other objects.
Example:
A Dog
object's behavior might include methods like bark()
, fetch()
, or wagTail()
that define what it can do.
Classes
Classes are blueprints or templates that define the structure and behavior for creating objects. They specify the data (instance variables) and actions (methods) that objects of that class will possess.
Example:
The Car
class defines that all cars will have a color, make, model, and methods like start()
and stop()
.
Constructor Parameters
Constructor parameters are local variables defined in the constructor's signature that receive values passed in when a new object is created. These values are typically used to initialize the object's instance variables.
Example:
In public Dog(String name, int age)
, name
and age
are constructor parameters that will be used to set the dog's initial name and age.
Constructors
Constructors are special methods used to create new objects (instances) of a class and initialize their instance variables to an initial state. They have the same name as the class and no return type.
Example:
When you write new Circle(5.0);
, you are calling the Circle
constructor to create a new circle object with a radius of 5.0.
Default Constructor
A default constructor is a no-argument constructor that Java automatically provides for a class if no other constructors are explicitly defined. It initializes instance variables to their default values (e.g., 0 for int, false for boolean, null for objects).
Example:
If you create a Person
object with new Person();
and haven't written any constructors, Java uses the default constructor to set int
fields to 0 and String
fields to null
.
Instance Variables
Instance variables are attributes declared within a class that define the data or state of each object created from that class. Each object gets its own copy of these variables.
Example:
In a Book
class, title
, author
, and numPages
would be instance variables, each holding unique values for different book objects.
Local Variables
Local variables are declared within a method, constructor, or block of code, and they exist only within that specific scope. They are temporary and are not part of an object's state.
Example:
In a method calculateArea(double length, double width)
, length
and width
are local variables that only exist while the method is executing.
Mutable Objects
Mutable objects are objects whose state (the values of their instance variables) can be changed after they are created. Examples include `ArrayList` or custom classes with setter methods.
Example:
An ArrayList
is a mutable object because you can add or remove elements from it after it's been created, changing its internal state.
New Keyword
The `new` keyword is used to create a new instance of a class (an object) in memory. It allocates memory for the object and then calls the class's constructor to initialize it.
Example:
The line Robot myRobot = new Robot();
uses the new keyword to create a fresh Robot
object in memory.
Null
`null` is a special literal that indicates a reference variable does not currently refer to any object. It signifies the absence of an object reference.
Example:
If a Student
object doesn't have an assigned textbook
yet, the textbook
instance variable might be initialized to null.
Objects
Objects are individual instances created from a class blueprint. Each object has its own unique set of data values for the instance variables defined by its class.
Example:
A specific red Honda Civic is an object created from the Car
class, with its own unique color, make, and model values.
Private Keyword
The `private` keyword is an access modifier that restricts access to a class's members (instance variables or methods) so they can only be accessed from within the class itself. This promotes encapsulation.
Example:
Declaring private int balance;
in a BankAccount
class ensures that the balance can only be changed by methods within the BankAccount
class, like deposit()
or withdraw()
, not directly from outside.
Reference Types
Reference types are data types that store references (memory addresses) to objects, rather than the actual data values themselves. Examples include `String`, arrays, and all custom classes.
Example:
When you declare String name = "Alice";
, name
is a reference type variable that holds the memory address where the "Alice" string object is stored.
State
The state of an object refers to the current values stored in its instance variables at any given time. It represents the data that defines the object's characteristics.
Example:
If a Player
object has health = 100
and score = 500
, that represents its current state in the game.