Sign in to follow this  

How to access to methods in a private constructor class

This topic is 1417 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i have found some librarys to work with dds textures in java, in one of them i have found a class that has a private constructor as i can´t create an instance of that class i would like to know how can i access to its methods.

Here is the code of that class:  https://github.com/Dahie/DDS-Utils/blob/master/DDSUtils/src/ddsutil/DDSUtil.java

Share this post


Link to post
Share on other sites

The constructor is private because the writer(s) didn't want you to be able to instantiate it. Instead they used static methods. This means that instead of:

DDSUtil util = new DDSUtil(); // If the constructor were public and didn't use static methods
BufferedImage image = util.read(file); // Requires a try { } catch { } too

You should actually write:

BufferedImage image = DDSUtil.read(file); // But don't forget try and catch

The same applies for all static members - you refer to them by the class name, not an instance of the object.

Share this post


Link to post
Share on other sites

Just to add to dr01d3k4's explanation, static members are class members and therefore accessed through the class, non-static members are object (instance) members and are accessed via an object / instance.

Share this post


Link to post
Share on other sites

This topic is 1417 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this