Java Programming Fundamentals

Intro To Classes & Objects

Basics of Classes and Objects

Java is an object-oriented programming language that centers around classes, objects, and methods.

Classes and Objects: The Basics

Class: A blueprint that defines the attributes and behaviors of objects but doesn't represent an actual instance.
Object: An instance of a class, representing a specific entity with defined attributes and behaviors.

Example: The Dog Class

Attributes: name, color, age.
Methods: bark(), walk().

Methods: Defining Behaviors

Methods: Actions or behaviors that objects can perform.
Parameters and Return Types: Methods can take parameters and return values.

'new' and 'static' in Java

Creating Objects

new keyword: Used to create new objects, allocating memory in the heap.

The 'static' Keyword

static variables/methods: Belong to the class rather than any instance, meaning they are shared across all objects of the class.

Basics of Java Constructors

Constructors

Special methods used to initialize objects.
No-argument constructor: A constructor that doesn't take any parameters.
Parameterized constructor: A constructor that takes parameters to set initial values for attributes.

this Keyword

Refers to the current instance of the class, used to differentiate between class attributes and parameters with the same name.

Static Variables

Shared by all instances of a class and can be accessed using the class name.

Primitive vs. Non-Primitive Data Types

Primitive Data Types

Basic data types like int, double, boolean.
Directly store values.

Non-Primitive Data Types

More complex types like String, arrays, and custom objects.
Store references to data.

Default Values

Primitive types: Have default values (e.g., 0 for int).
Non-Primitive types: Default to null.

The Scope of Variables

Local Variables

Defined within methods or blocks, accessible only within those blocks.

Method Parameters

Passed into methods and accessible only within those methods.

Instance Variables

Declared inside a class but outside any method, tied to objects of the class.

Static (Class) Variables

Declared inside a class with the static keyword, shared by all instances of the class and accessible using the class name.
These concepts of Classes & Objects are very useful for understanding core Java. For more detailed information, refer to the Notion link:
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.