0% found this document useful (0 votes)
4 views2 pages

Java Self Notes

The document explains the concepts of classes and objects in Java, using real-world analogies like cookie cutters and baked cookies to illustrate the difference between a class as a blueprint and an object as a tangible instance. It also discusses three types of variables in Java: local variables, instance variables, and class (static) variables, detailing their definitions, scopes, lifetimes, and initialization. Understanding these concepts is fundamental to Java and object-oriented programming, promoting modular and organized code.

Uploaded by

jazaamujawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Java Self Notes

The document explains the concepts of classes and objects in Java, using real-world analogies like cookie cutters and baked cookies to illustrate the difference between a class as a blueprint and an object as a tangible instance. It also discusses three types of variables in Java: local variables, instance variables, and class (static) variables, detailing their definitions, scopes, lifetimes, and initialization. Understanding these concepts is fundamental to Java and object-oriented programming, promoting modular and organized code.

Uploaded by

jazaamujawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

class and object

Class (Think: A cookie cutter or blueprint)

 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.

Object (Think: The actual cookie or the built house)

 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

 Definition: Variables declared inside a method, constructor, or a block of code.

 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.

 Scope: Accessible throughout the class.

 Lifetime: Created when an object is instantiated and destroyed when the object is garbage
collected.

 Initialization: Have default values based on their data type.

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.

3. Class Variables (Static Variables)

 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

You might also like