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 OverflowWhat is a class in Java and what does it have to do with the filename?
How do I actually use a class in Java?
Java Classes and Objects Explained with Examples - Guide - The freeCodeCamp Forum
What is an anonymous inner class, and why are they useful?
Videos
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());
.class is used when there isn't an instance of the class available.
.getClass() is used when there is an instance of the class available.
object.getClass() returns the class of the given object.
For example:
String string = "hello";
System.out.println(string.getClass().toString());
This will output:
class java.lang.String
This is the class of the string object :)
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.
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!