When you write .class after a class name, it references the class literal - java.lang.Class object that represents information about a given class.

For example, if your class is Print, then Print.class is an object that represents the class Print on runtime. It is the same object that is returned by the getClass() method of any (direct) instance of Print.

Print myPrint = new Print();
System.out.println(Print.class.getName());
System.out.println(myPrint.getClass().getName());
Answer from Javier on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Class.html
Class (Java Platform SE 8 )
October 20, 2025 - Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-class-class-java-set-1
Java.lang.Class class in Java | Set 1 - GeeksforGeeks
March 14, 2023 - Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.
Discussions

What is a class in Java and what does it have to do with the filename?
I'm surprised you can't find anything from google, since this is pretty basic Java. Literally every tutorial starts with this.... Anyway, think of a class like a blueprint. Let's call it, "Car." Car has features to it, like Year, Color, Make, fourWheelDrive, etc. When I want to make a Car, I consult the blueprints, filling in those details for Year, Make, etc. When that Car is done, I make a new one, go to blueprints, rinse repeat. Let's say I have another class called Dog. Dog has different parameters than Car. Dog has things like Breed, Size, isFluffy, isGoodBoi, etc. If I go to the blueprints for Dog and I try to plug in fourWheelDrive = true, well, that doesn't apply to Dog. Just as isFluffy does apply to Car. I hope that makes sense. I'm sure someone will expand on this example if it doesn't . More on reddit.com
🌐 r/programminghelp
2
3
January 20, 2021
How do I actually use a class in Java?
You're a transport company and you have lots of cars that you rent to people. Each car has information related to it like make/model, mileage, year of purchase, rental rate .... etc. You can represent all of that as a class Car which has properties that depict all those relevant bits of data. Same goes for a User class though for security reasons passwords are not stored ever. Instead a hash of the password is stored. You don't make a class for each individual user. Rather you make the User class for all users and each instance of that class is an individual user. More on reddit.com
🌐 r/learnprogramming
4
2
November 23, 2022
Java Classes and Objects Explained with Examples - Guide - The freeCodeCamp Forum
Classes and Objects Classes are groups of variables and operations on them. A class can have variables, methods (or functions), and constructors (or methods which are used to initiate, more on that later!). A class may contain any of the following variable types. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
July 17, 2019
What is an anonymous inner class, and why are they useful?
As the name suggests, it is not a named class, hence the anonymous. button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // This is an anonymous inner class } }); See how instead of creating a named ActionListener class separately I am using the curly braces to create a new, anonymous instance. This is useful if you have a method that takes a class that may just simply be a method-wrapper, or in an instance where creating a new named class may be very verbose. You can also use it to create a named variable of a class. ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { // This is an anonymous inner class } } I've worked with enterprise code for 3 years now and I see these pop up rarely, usually only in instances I've named above. Otherwise, it is best to actually create a class with a more explicit purpose, something that is named appropriately with proper documentation. More on reddit.com
🌐 r/java
26
10
September 17, 2014
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › Class.html
Class (Java SE 11 & JDK 11 )
January 20, 2026 - Instances of the class Class represent classes and interfaces in a running Java application. An enum type is a kind of class and an annotation type is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions.
🌐
Java
download.java.net › java › early_access › valhalla › docs › api › java.base › java › lang › Class.html
Class (Java SE 23 & JDK 23 [build 1])
Instances of the class Class represent classes and interfaces in a running Java application. An enum class and a record class are kinds of class; an annotation interface is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › java_lang_class.htm
java.lang.Class class
The java.lang.Class class instance represent classes and interfaces in a running Java application. It has no public constructor.
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.class
Class Class (Java.Lang) | Microsoft Learn
Instances of the class Class represent classes and interfaces in a running Java application. An enum class and a record class are kinds of class; an annotation interface is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions.
🌐
Reddit
reddit.com › r/programminghelp › what is a class in java and what does it have to do with the filename?
r/programminghelp on Reddit: What is a class in Java and what does it have to do with the filename?
January 20, 2021 -

Hello! I'm new to Java programming and I'm having some trouble understanding what a class is. What exactly is a class and what does it do? Why does the filename have to match the class name and what is considered as matching? I've tried googling it before but I still don't really get it.

Top answer
1 of 2
2
I'm surprised you can't find anything from google, since this is pretty basic Java. Literally every tutorial starts with this.... Anyway, think of a class like a blueprint. Let's call it, "Car." Car has features to it, like Year, Color, Make, fourWheelDrive, etc. When I want to make a Car, I consult the blueprints, filling in those details for Year, Make, etc. When that Car is done, I make a new one, go to blueprints, rinse repeat. Let's say I have another class called Dog. Dog has different parameters than Car. Dog has things like Breed, Size, isFluffy, isGoodBoi, etc. If I go to the blueprints for Dog and I try to plug in fourWheelDrive = true, well, that doesn't apply to Dog. Just as isFluffy does apply to Car. I hope that makes sense. I'm sure someone will expand on this example if it doesn't .
2 of 2
1
Quick start to object oriented programming (using classes): The most basic thing you can do with classes is create custom types. For example, if I wanted a fraction, I could write a class that groups 2 integers together: class Fraction { public int numerator; public int denominator; } now instead of having to keep track of the numerator and denominator of a fraction separately, they're grouped together into a class called a fraction, and you can use this later in your code: Fraction f = new Fraction(); f.numerator = 1; f.denominator = 2; There is a lot of things that you can do with objects. Methods add functionality. I could add a simplify method to my fraction class, which of course would simplify the fraction: 2/4 -> 1/2:f.simplify(); The code would look something like this: class Fraction{ public int numerator; public int denominator; public void simplify(){ {{ CODE THAT SIMPLIFIES THE FRACTION BY MODIFYING numerator AND denominator}} } } That should be enough to get you started with learning this stuff. It's not a complete guide, not at all, but this is probably the best way I found to think about classes when I started learning. Later on you'll learn more, like why declaring the numerator and denominator fields (variables inside classes are called fields) as public is bad practice, which is something that might get you in trouble with the old, veterans of the industry types who think they're smarter than you. (it's fine for now, don't worry about bad practice while you're learning. Do whatever you want for now, learn good practice later; explore, have fun, don't be scared of your own code). also, about the file name matching, it's just a java thing, other language have classes too, and they don't require it, like C#. There isn't really any reasoning behind it other than keeping your project organized. I think it has something to do with how the compiler works.
🌐
W3Schools
w3schools.com › java › java_classes.asp
Java Classes and Objects
Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object.
🌐
Scaler
scaler.com › topics › class-class-in-java
Class Class in Java| Scaler Topics
March 23, 2023 - The Class class present in Java has a private constructor, so a new keyword does not help in creating the instance of it, instead of it we use three methods for creating the object of the class.
🌐
Reddit
reddit.com › r/learnprogramming › how do i actually use a class in java?
r/learnprogramming on Reddit: How do I actually use a class in Java?
November 23, 2022 -

I'm sorry if this is gonna sound incredibly stupid...

In Java, we were taught classes, for example, class Car. Then in our constructor, we add wheels, engines and all the other things that make up a car.

I know exactly how to do it when it comes to these examples, but I couldn't know if real-life logic merits the use of a class.

Can you give me an example of real-world use of a class?

For example, if I am creating a user authentication API, should I make classes for individual users? Their usernames, passwords, and other information stored in an object instance? My gut says no so please help me out. Thanks in advance!

Top answer
1 of 4
3
You're a transport company and you have lots of cars that you rent to people. Each car has information related to it like make/model, mileage, year of purchase, rental rate .... etc. You can represent all of that as a class Car which has properties that depict all those relevant bits of data. Same goes for a User class though for security reasons passwords are not stored ever. Instead a hash of the password is stored. You don't make a class for each individual user. Rather you make the User class for all users and each instance of that class is an individual user.
2 of 4
2
I think sometimes it's helpful to forget the whole "model the real world" aspect and instead just focus on how a class can make your program easier to read and easier to work with. You can start with all of your code in one class and then only add new classes as needed. Do you understand how to use a function or method to avoid duplicate code? For example, if you find yourself writing 5 lines of code to read user settings, and then after that you have 5 lines of almost identical code to read system settings - you can make a method to do the same and just call the method twice? Think of classes as just a fancier way to do that. Instead of making a method to save some repetitive code, you can create a class, which lets you bundle up a collection of methods and the data associated with those. Basically, if you have code all in one class, and then you refactor your code to use an additional class, it should make your program simpler and easier to read and easier to modify. You should have less duplication and less redundancy. If that's not the case - if adding a class made your code longer and harder to follow - then it wasn't worth it.
🌐
GeeksforGeeks
geeksforgeeks.org › java › classes-objects-java
Classes and Objects in Java - GeeksforGeeks
Note: Every class has at least one constructor. If none is defined Java provides a default no-argument constructor that calls the parent constructor.
Published   January 20, 2026
🌐
freeCodeCamp
forum.freecodecamp.org › guide
Java Classes and Objects Explained with Examples - Guide - The freeCodeCamp Forum
July 17, 2019 - Classes and Objects Classes are groups of variables and operations on them. A class can have variables, methods (or functions), and constructors (or methods which are used to initiate, more on that later!). A class may …
🌐
Tutorialspoint
tutorialspoint.com › home › java › java object classes
Java Object Classes
September 1, 2008 - Following is the EmployeeTest class, which creates two instances of the class Employee and invokes the methods for each object to assign values for each variable. Save the following code in EmployeeTest.java file.
🌐
DataCamp
datacamp.com › doc › java › classes-and-objects
Java Classes and Objects
A class in Java defines a new data type that can include fields (variables) and methods to define the behavior of the objects created from the class.
🌐
Codecademy
codecademy.com › docs › java › classes
Java | Classes | Codecademy
September 4, 2024 - In Java, classes are blueprints or templates for objects in Java. They detail the general structure and data for an object including information such as properties, attributes, and method behavior.
🌐
OnlineGDB
question.onlinegdb.com › 16679 › what-is-class-in-java
What is Class in Java? - OnlineGDB Q&A
September 24, 2024 - Classes in object-oriented programming are the fundamentals of encapsulation. Classes hold functionality that can be grouped to organize your code logically · For instance, if you have functionality that helps you maintain employee information (name, address, ID, taxId, etc) and you have ...
🌐
Quora
quora.com › What-does-it-mean-in-Java-Class-class-new-Class
What does it mean in Java? 'Class class = new Class()'? - Quora
Answer (1 of 7): In the class name Class an object is made into a heap with pointer variable (reference variable) i.e class that is a present into a stack which points the address of an object present into the heap . Here new keyword is used to allocate some memory for an object into a heap. The...