- 1Java Exceptions
- 2Java Keywords
- 3Java abstract Keyword
- 4Java assert Keyword
- 5Java boolean Keyword
- 6Java break Keyword
- 7Java byte Keyword
- 8Java case Keyword
- 9Java catch Keyword
- 10Java char Keyword
- 11Java class Keyword
- 12Java const Keyword
- 13Java continue Keyword
- 14Java default Keyword
- 15Java do Keyword
- 16Java double Keyword
- 17Java else Keyword
- 18Java enum Keyword
- 19Java extends Keyword
- 20Java final Keyword
- 21Java finally Keyword
- 22Java float Keyword
- 23Java for Keyword
- 24Java goto Keyword
- 25Java if Keyword
- 26Java implements Keyword
- 27Java import Keyword
- 28Java instanceof Keyword
- 29Java int Keyword
- 30Java interface Keyword
- 31Java long Keyword
- 32Java native Keyword
- 33Java new Keyword
- 34Java null Keyword
- 35Java package Keyword
- 36Java private Keyword
- 37Java protected Keyword
- 38Java public Keyword
- 39Java return Keyword
- 40Java short Keyword
- 41Java static Keyword
- 42Java strictfp Keyword
- 43Java super Keyword
- 44Java switch Keyword
- 45Java synchronized Keyword
- 46Java this Keyword
- 47Java transient Keyword
- 48Java try Keyword
- 49Java void Keyword
- 50Java volatile Keyword
- 51Java while Keyword
- 52Java String Methods - Syntax and Description
- 53Java String
charAt()
method - 54Java String
codePointAt()
method - 55Java String
codePointBefore()
method - 56Java String
codePointCount()
method - 57Java String
compareTo()
method - 58Java String
compareToIgnoreCase()
method - 59Java String
concat()
method - 60Java String
contains()
method - 61Java String
contentEquals()
method - 62Java String
copyValueOf()
method - 63Java String
endsWith()
method - 64Java String
equals()
method - 65Java String
equalsIgnoreCase()
method - 66Java String
format()
method - 67Java String
getBytes()
method - 68Java String
getChars()
method - 69Java String
hashCode()
method - 70Java String
indexOf()
method - 71Java String
intern()
method - 72Java String
isEmpty()
method - 73Java String
join()
method - 74Java String
lastIndexOf()
method - 75Java String
length()
method - 76Java String
matches()
method - 77Java String
offsetByCodePoints()
method - 78Java String
regionMatches()
method - 79Java String
replace()
method - 80Java String
replaceAll()
method - 81Java String
replaceFirst()
method - 82Java String
split()
method - 83Java String
startsWith()
method - 84Java String
subSequence()
method - 85Java String
substring()
method - 86Java String
toCharArray()
method - 87Java String
toLowerCase()
method - 88Java String
toString()
method - 89Java String
toUpperCase()
method - 90Java String
trim()
method - 91Java String
valueOf()
method - 92Java ArrayList Methods - Complete Reference with Syntax and Description
- 93Java LinkedList Methods - Complete Reference with Syntax and Description
- 94Java HashMap Methods - Syntax and Descriptions
Java class
Keyword
Usage and Examples
class
Keyword in Java
The class
keyword in Java is one of the fundamental building blocks of the language. It is used to define a blueprint for objects. Think of it as a mold — it doesn't hold values itself, but it tells Java what an object of that class should contain and be able to do.
What is a Class in Java?
A class in Java is a user-defined data type that encapsulates data (fields) and operations (methods). It represents real-world entities like Student, Car, or BankAccount. The class
keyword is how you start defining one.
Syntax of the class
Keyword
class ClassName {
// fields
// methods
}
Note: The name of the class should follow Java naming conventions — typically written in PascalCase.
Basic Example: Creating a Simple Class
class Dog {
String breed;
int age;
void bark() {
System.out.println("Woof!");
}
}
This Dog
class defines two fields (breed
and age
) and one method (bark()
).
Using the Class in a Java Program
Defining a class alone does not execute anything. You must create an object from it and use its members:
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog(); // creating an object of Dog
myDog.breed = "Labrador";
myDog.age = 3;
System.out.println("Breed: " + myDog.breed);
System.out.println("Age: " + myDog.age);
myDog.bark();
}
}
Breed: Labrador
Age: 3
Woof!
Important Concepts Around the class
Keyword
1. Class vs. Object
The class
defines the structure. An object is an actual instance of that structure. One class can be used to create multiple objects.
2. Class with Constructor
You can add a constructor to initialize object values at the time of creation:
class Student {
String name;
int rollNo;
Student(String n, int r) {
name = n;
rollNo = r;
}
void display() {
System.out.println("Name: " + name + ", Roll No: " + rollNo);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Alice", 101);
s1.display();
}
}
Name: Alice, Roll No: 101
3. Public, Default, and Final Classes
- public class: Can be accessed from any other class.
- default class (no modifier): Accessible only within its package.
- final class: Cannot be extended (inherited).
final class Utility {
// cannot be extended
}
4. Nested Classes
You can define a class inside another class. These are called nested or inner classes.
class Outer {
class Inner {
void sayHello() {
System.out.println("Hello from Inner class!");
}
}
}
public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.sayHello();
}
}
Hello from Inner class!
Best Practices When Using class
- Use meaningful names that reflect the class's purpose (e.g.,
Customer
,Invoice
). - Encapsulate fields using
private
and expose them through getters and setters. - Keep one public class per file (recommended).
- Follow naming conventions: class names should begin with uppercase letters.