Java Self Notes
Java Self Notes
Real-world analogy: Imagine a cookie cutter. It defines the shape, size, and design of a
cookie. It's a template, but it's not an actual cookie you can eat.
Another analogy: Think of a blueprint for a house. The blueprint specifies the number of
rooms, the layout, the size of the windows, and the materials needed. It's a design, a plan,
but it's not a house you can live in yet.
Java context: In Java, a class is a blueprint or template for creating objects. It defines the
characteristics (data or fields/attributes) and behaviors (methods or functions) that objects
of that class will possess. A class doesn't occupy memory itself until an object is created from
it.
Real-world analogy: An actual cookie you bake using the cookie cutter. You can eat it, it has a
specific flavor, and it exists in the real world.
Another analogy: The actual house built according to the blueprint. This house has a specific
address, paint color, furniture, and people living in it. It's a real, tangible entity.
Java context: An object is an instance of a class. It's a tangible entity created from the class
blueprint, having its own unique set of values for the attributes defined by the class. An
object occupies memory when it's created (instantiated).
Key takeaway
A class defines the possibilities, the structure, and the potential actions.
An object is the realization of those possibilities, a concrete instance that possesses specific
data and can perform actions.
You define a class once, but you can create many objects (instances) from that single class, each with
its unique data. This concept is fundamental to Java and object-oriented programming, allowing for
modular, reusable, and organized code.
variables in java
1. Local Variables
Scope: They are only accessible within the method, constructor, or block where they are
declared.
Lifetime: They are created when the method, constructor, or block is entered and destroyed
once it exits the method, constructor, or block.
Initialization: Unlike instance or static variables, local variables are not given a default value
and must be initialized before use, states learnjava.co.in.
An example of local variables can be found in the provided Java code snippet. In this example, a is
local to myMethod(), while b is local to the if block, demonstrating their limited scope.
2. Instance Variables
Definition: Variables declared within a class but outside any method, constructor, or block.
Lifetime: Created when an object is instantiated and destroyed when the object is garbage
collected.
An example is shown in the provided code snippet, where brand, year, and isElectric are instance
variables, with each Car object having its own copy.
Definition: Variables declared with the static keyword within a class but outside any method
or constructor.
Scope: Belong to the class and are shared among all instances.
Lifetime: Created when the class is loaded and exist as long as the program runs.
Initialization: Have default values and can be initialized in static initializer blocks