DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Why does Void class exist in JDK

Why does Void class exist in JDK

Sandeep Bhandari user avatar by
Sandeep Bhandari
·
May. 03, 11 · Java Zone · Interview
Like (2)
Save
Tweet
22.29K Views

Join the DZone community and get the full member experience.

Join For Free

I always try to bring some thing new and useful on this blog. This time we will understand the Void.class (which in itself looks something tricky) present in rt.jar.

One can consider the java.lang.Void class as a wrapper for the keyword void.
Some developers draw the analogy with the primitive data types int, long, short and byte etc. which have the wrapper classes as Integer, Long, Short and Byte receptively. But it should be kept in mind that unlike those wrappers Void class doesn't store a value of type void in itself and hence is not a wrapper in true essence.

Purpose:

The Void class according to javadoc exists because of the fact that some time we may need to represent the void keyword as an object. But at the same point we cannot create an instance of the Void class using the new operator.
This is because the constructor in Void has been declared as private.
Moreover the Void class is a final class which means that there is no way we can inherit this class.

So the only purpose that remains for the existence of the Void class is reflection, where we can get the return type of a method as void. The following piece of code will demonstrate this purpose:

public class Test {
public static void main(String[] args) throws SecurityException, NoSuchMethodException {
Class c1 = Test1.class.getMethod("Testt",null).getReturnType();
System.out.println(c1 == Void.TYPE);
System.out.println(c1 == Void.class);
}
}

class Test1{
public void Testt(){}
}

One can also use Void class in Generics to specify that you don't care about the specific type of object being used. For example:

List<void> list1;

 

From http://extreme-java.blogspot.com/2011/04/void-class-java.html

Java Development Kit Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Best JavaScript Web Development Frameworks
  • Build a Data Pipeline on AWS With Kafka, Kafka Connect, and DynamoDB
  • Is NoOps the End of DevOps?
  • Artificial Intelligence (AI) And Its Assistance in Medical Diagnosis

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo