Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a "runtime class" in Java?

Tags:

java

oop

I try to understand what the Object.getClass() method does.

The documentation says that it "returns the runtime class of an object." That explanation doesn't help me understanding the term.

Has someone a simple description of what a "runtime class" is and what getClass() does?

like image 699
cluster1 Avatar asked Nov 08 '16 12:11

cluster1


People also ask

What is the use of runtime in Java?

The Java Runtime Environment (JRE) is software that Java programs require to run correctly. Java is a computer language that powers many current web and mobile applications. The JRE is the underlying technology that communicates between the Java program and the operating system.

What is Java Runtime called?

The Java Runtime Environment, or JRE, is a software layer that runs on top of a computer's operating system software and provides the class libraries and other resources that a specific Java program needs to run. The JRE is one of three interrelated components for developing and running Java programs.

What is the purpose of the runtime class and system class?

Answer: The purpose of the Runtime class is to provide access to the Java runtime system. The runtime information like memory availability, invoking the garbage collector, etc. The purpose of the System class is to provide access to system resources.

What is runtime error in Java?

A runtime error in Java is an application error that occurs during the execution of a program. A runtime error occurs when a program is syntactically correct but contains an issue that is only detected during program execution.


2 Answers

Just understand it as "an object that has all the metadata of the object's type". In that object, you can find the methods declared in the class, the fields, the type hierarchy, etc. This information will be typically used by code that uses reflection to either inspect objects/types or to run method without the need to have the class defined and compiled when they, themselves are being coded.

"Runtime" may be emphasized because the class definition may change over time, or the object may be declared as a supertype while it actually is an instance of a subtype of the one declared. When a certain class is loaded, it's that information, as loaded during that instance, that will be returned by the getClass() method.

In short, when your code runs, the VM will have a definition of your class in a different way than the "source" form that you type in a .java file. That information, of course after being compiled, will be loaded and all the metadata (as said above) will constitute what they call the "runtime class". It's just a fancy way to say "an object with all the metadata about a class loaded when the program is running"

like image 151
ernest_k Avatar answered Sep 21 '22 15:09

ernest_k


It means "the class of the instance the variable refers to at runtime" (sorry if that's not actually clearer).

If you have a reference to an Object, it could refer to an Object, a String, an Integer... you get that class, not Object.

Object obj1 = new Object();
System.out.println(obj1.getClass());  // java.lang.Object

String obj2 = "";
System.out.println(obj2.getClass());  // java.lang.String

obj1 = obj2;
System.out.println(obj1.getClass());  // java.lang.String, not Object.
like image 35
Andy Turner Avatar answered Sep 20 '22 15:09

Andy Turner