
Java is an Object-Oriented programming language. In Java, the classes and objects are the basic and important features of object-oriented programming system, Java supports the following fundamental OOPs concepts
Classes
Objects
Inheritance
Polymorphism
Encapsulation
Abstraction
Instance
Method
Message Passing
In this tutorial, we will learn about Java Classes and Objects, the creation of the classes and objects, accessing class methods, etc.
What are Java Classes?
A class is a blueprint from which individual objects are created (or, we can say a class is a data type of an object type). In Java, everything is related to classes and objects. Each class has its methods and attributes that can be accessed and manipulated through the objects.
For example, if you want to create a class for students. In that case, "Student" will be a class, and student records (like student1, student2, etc) will be objects.
A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known as functions.
Why use methods? To reuse code: define the code once, and use it many times.
Create a Method
A method must be declared within a class. It is defined with the name of the method, followed by parentheses (). Java provides some pre-defined methods, such as System.out.println(), but you can also create your own methods to perform certain actions:
Type Description No parameters, no return value Just performs an action With parameters, no return value Takes input, no result back No parameters, with return value Returns a result, but no input With parameters and return value Takes input and returns result
class Demo {
void show() {
System.out.println("No parameters");
}
void show(int a) {
System.out.println("One parameter: " + a);
}
void show(String s, int a) {
System.out.println("String and int: " + s + ", " + a);
}
}
Encapsulation
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must:
declare class variables/attributes as private
provide public get and set methods to access and update the value of a private variable
Get and Set
You learned from the previous chapter that private variables can only be accessed within the same class (an outside class has no access to it). However, it is possible to access them if we provide public get and set methods.
The get method returns the variable value, and the set method sets the value.
Syntax for both is that they start with either get or set, followed by the name of the variable, with the first letter in upper case:
public class Person {
private String name; // private = restricted access
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
this.name = newName;
}
}
Encapsulation in Java is one of the fundamental principles of object-oriented programming (OOP). It refers to the concept of wrapping the data (variables) and the code (methods) that operates on the data into a single unit, typically a class. By making variables private and providing public getter and setter methods, Java allows controlled access to the fields of a class, ensuring data security and integrity. This mechanism helps protect the internal state of an object from unintended interference and misuse. Encapsulation promotes modularity and maintainability, as it hides the implementation details and exposes only what is necessary for the outside world. This makes it easier to make changes to a class without affecting other parts of the program that use the class. Overall, encapsulation helps in building robust and secure Java applications.
Java Conditions and If Statements
You already know that Java supports the usual logical conditions from mathematics:
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Equal to a == b
Not Equal to: a != b
You can use these conditions to perform different actions for different decisions.
Java has the following conditional statements:
Use if to specify a block of code to be executed, if a specified condition is true
Use else to specify a block of code to be executed, if the same condition is false
Use else if to specify a new condition to test, if the first condition is false
Use switch to specify many alternative blocks of code to be executed
Java Switch Statements
Instead of writing many if..else statements, you can use the switch statement.
The switch statement selects one of many code blocks to be executed:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Java Data Types
As explained in the previous chapter, a variable in Java must be a specified data type:
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99f; // Floating point number
char myLetter = 'D'; // Character
boolean myBool = true; // Boolean
String myText = "Hello"; // String
Data types are divided into two groups:
Primitive data types - includes byte, short, int, long, float, double, boolean and char
Non-primitive data types - such as String, Arrays and Classes (you will learn more about these in a later chapter)
Primitive Data Types
A primitive data type specifies the type of a variable and the kind of values it can hold.
There are eight primitive data types in Java:
Data TypeDescriptionbyteStores whole numbers from -128 to 127shortStores whole numbers from -32,768 to 32,767intStores whole numbers from -2,147,483,648 to 2,147,483,647longStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807floatStores fractional numbers. Sufficient for storing 6 to 7 decimal digitsdoubleStores fractional numbers. Sufficient for storing 15 to 16 decimal digitsbooleanStores true or false valuescharStores a single character/letter or ASCII values
Java is a powerful, versatile, and platform-independent programming language widely used for building robust, high-performance applications. Originally developed by Sun Microsystems and now maintained by Oracle, Java follows the object-oriented programming (OOP) paradigm, which promotes code reusability, scalability, and maintainability. It enables developers to write code once and run it anywhere, thanks to the Java Virtual Machine (JVM), which allows Java programs to run on any device or operating system. Java is commonly used for developing desktop applications, web applications, mobile apps (especially Android), and enterprise-level systems. With its rich set of APIs, extensive libraries, and frameworks such as Spring and Hibernate, Java continues to be a top choice for software development across industries. Its strong memory management, built-in security features, and support for multithreading make it suitable for both small-scale and large-scale systems.
Welcome to the Complete Java Masterclass, the only course you need to master Java development from the ground up. This comprehensive program is designed to take you from a complete beginner to an advanced-level Java developer by combining a perfect balance of theory, practical projects, and professional-level best practices.
We will begin with Java fundamentals—variables, data types, control statements, and loops—ensuring a solid understanding of the language basics. As we progress, you'll learn the ins and outs of object-oriented programming, error handling, collections, file handling, and much more.
Videos
Starting at the basics and working our way to more advanced topics
Easily digestible videos which tell you what you need to know without long-winded explanations or digressions
Concepts described in detail without straying to far from the topic at hand