Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, a class is the fundamental building block for object-oriented programming. It serves as a blueprint for creating objects (instances) with specific attributes and behavior. In this tutorial, we will cover how to create a simple class in Java, define attributes and methods, and create objects from the class.
To define a class in Java, use the class
keyword followed by the class name. The class name should be a noun that starts with a capital letter (PascalCase). The class body is enclosed in curly braces {}
.
Example:
public class Person { // Class body }
Attributes are variables defined inside the class that represent the state of an object. They can have different access levels: public
, private
, or protected
.
In most cases, attributes should be private
to ensure data encapsulation.
Example:
public class Person { private String name; private int age; }
Methods define the behavior of a class. They can perform actions, manipulate attributes, or communicate with other objects. Just like attributes, methods can have different access levels.
In this example, we will create a getter and a setter for each attribute.
Example:
public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
To create an object (instance) of a class, use the new
keyword followed by the class constructor. In this example, we are using the default constructor, which is automatically created by the Java compiler.
Example:
public class Main { public static void main(String[] args) { Person person1 = new Person(); person1.setName("John"); person1.setAge(30); System.out.println("Person 1: " + person1.getName() + ", age " + person1.getAge()); // Output: Person 1: John, age 30 Person person2 = new Person(); person2.setName("Jane"); person2.setAge(28); System.out.println("Person 2: " + person2.getName() + ", age " + person2.getAge()); // Output: Person 2: Jane, age 28 } }
In summary, creating a class in Java involves defining the class using the class
keyword, adding attributes and methods, and creating objects using the new
keyword and the class constructor. This tutorial demonstrates the basic structure of a class, but classes can also include constructors, static attributes, static methods, and inner classes to create more complex behaviors.
Defining a class in Java with examples
A class is a blueprint for creating objects. Here's a simple example:
public class Dog { // Fields (attributes) String name; int age; // Constructor public Dog(String name, int age) { this.name = name; this.age = age; } // Method public void bark() { System.out.println("Woof!"); } }
Java class structure and components
A Java class consists of fields (attributes), constructors, and methods. Here's a breakdown:
public class MyClass { // Fields (attributes) int myField; // Constructor public MyClass(int myField) { this.myField = myField; } // Method public void myMethod() { // Method implementation } }
Constructors and methods in Java class creation
Constructors initialize the object, and methods define the behavior. Example:
public class Student { // Fields String name; int age; // Constructor public Student(String name, int age) { this.name = name; this.age = age; } // Method public void study() { System.out.println(name + " is studying."); } }
Creating and instantiating objects in Java
To use a class, you create objects. Example:
public class Main { public static void main(String[] args) { Student student1 = new Student("Alice", 20); Student student2 = new Student("Bob", 22); student1.study(); student2.study(); } }
Public vs. private access modifiers in Java classes
Access modifiers control the visibility of fields and methods. Example:
public class Car { // Public field public String model; // Private field private int year; // Public method public void start() { System.out.println("Car started."); } // Private method private void accelerate() { System.out.println("Car accelerating."); } }
Inheritance and polymorphism in Java classes
Inheritance allows a class to inherit properties and behaviors from another class. Polymorphism enables objects to take on multiple forms. Example:
public class Animal { public void sound() { System.out.println("Animal makes a sound"); } } public class Dog extends Animal { // Overriding method @Override public void sound() { System.out.println("Dog barks"); } }
Encapsulation in Java classes
Encapsulation involves bundling data and methods that operate on the data within a single unit. Example:
public class BankAccount { private double balance; // Getter method public double getBalance() { return balance; } // Setter method public void setBalance(double balance) { if (balance >= 0) { this.balance = balance; } else { System.out.println("Invalid balance"); } } }