Runtime class in Java

Previous
Next

Runtime class:

  • It is pre-defined class.
  • It is belongs to java.lang package.
  • Runtime class represents Java Virtual Environment(JVM) instance.
  • For entire java application execution, only one JVM instance will be created.
  • It is singleton class, hence we cannot instantiate directly.
  • To get instance, we need to call factory method of Runtime class.
/*
class Runtime
{
	public static Runtime getRuntime()
	{
		Returns the runtime object associated with the current Java application. 
	}
}
*/
class RuntimeClass 
{
	public static void main(String[] args) 
	{
		// Runtime r = new Runtime(); -> Error :
		Runtime r1 = Runtime.getRuntime();
		Runtime r2 = Runtime.getRuntime();
		if(r1==r2)
			System.out.println("Runtime is Singleton");
		else
			System.out.println("Runtime is Factory");
	}
}
  • Runtime class is representing JVM (runtime instance of java application)
  • Every java application has single JVM instance, hence it is called Singleton.
class JVM
{
	public static void main(String[] args) 
	{
		Runtime jvm = Runtime.getRuntime();
	}
}
Previous
Next

Add Comment

Courses Enquiry Form