Jump to content

  • Log In with Google      Sign In   
  • Create Account


How to access to methods in a private constructor class


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 jor1980   Members   -  Reputation: 142

Like
0Likes
Like

Posted 01 March 2014 - 08:58 AM

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



Sponsor:

#2 dr01d3k4   Members   -  Reputation: 402

Like
2Likes
Like

Posted 01 March 2014 - 09:06 AM

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.


Falling block colour flood game thing I'm making: http://jsfiddle/dr01d3k4/JHnCV/


#3 de_mattT   Members   -  Reputation: 308

Like
0Likes
Like

Posted 01 March 2014 - 09:35 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS