[Solved] How to properly use type 'Object', or if there is a work around?

Thanks for clicking here, and to not waste your time, let's get to the point.

I'm currently working on a file library, because it will be handy in future builds. My way of doing this is to make an ArrayList of a class I've made to store information on the file.

long newID; long newID(){return newID++;} //Generates a new ID when called.

class file { //File class. Stores information about a file. String name, type, path; long ID; Object content;

file(String fileName, String filePath){ String temp[] = split(fileName,"."); //Splits the file name into name and type. name = temp[0]; //The name of the file, mostly for file search. path = filePath; //The file path on local machine. ID = newID(); //Generates a new ID for the file, could be handy. type = ""; //Empty means folder

if (temp.length > 1){ type = temp[1].toLowerCase(); //There is a file type, let's assign it. }

} }

To simplify my question: I would like my code to work like this:

file temp = new file("test.png", "C:\\test.png"); temp.content = loadImage(temp.path); image(temp.content, 0, 0, width, height);

The problem is 'temp.content' is not the type PImage, and I don't know a way to make it accepted, without declearing a new temporary PImage which loads the image instead, which is not acctually what I'm after. I want to load all files at launch, and then there should be no need to load them again. I could expand my class to contain all the different types, like PImage, sound, text, but that is just plain messy. I like it clean and simple. This operation works in most languages, so I was hoping so did Processing due to Java.

For those who wonder, I can simply use a if statement on the 'temp.type' to find out if it is a image, sound or whatever it is, so it won't create a runtime error.

Any help would be greatly appreciated, thanks for reading!


I'm bad at explaining, but what I meant is I want the 'Object content' to be a variable I can change. Pretty much like: 'content = new PImage' or 'content = new className'. Like it would change the type which content represented. It would be really handy, but not quite sure how to achieve this.


  • Well.. I don't get how much you will benefit from this what you are doing.. Anyway, when you use Object as a type, you need to explicitly cast a type. Not sure that will work in this context, but try: temp.content = (PImage) loadImage(temp.path);

  • edited December 2015 Answer ✓

    @Ater The benefit would be a easier script to read, as all object are assigned to one value in the ArrayList, and not multiple like: temp.image if image file, or, temp.sound. It looks good tho, but it will be a mess in the end if I need to do this for every type. The new way will be able to handle any class.

    While your answer didn't solve my problem right away, you gave me a hint. The code you have doesn't work like I hoped, as the image(PImage, int, int, int, int) class still wasn't calling it a PImage. However, I can use your trick in functions.

    temp.content = loadImage(temp.path); image( (PImage) temp.content, 0, 0, width, height);

    This example works. However, I will still run into problems with the type's functions. Like temp.content.updatePixels(); is not working due to not being of type PImage. Might be able to use a similiar trick here aswell, which would be great, but I can also create a function to do my stuff where I call the function with temp.content as a PImage.

    temp.content = loadImage(temp.path); imageHandler( (PImage) temp.content); void imageHandler(PImage temp) { temp.updatePixels(); }

    Anyway, thanks for your help! You really helped me out here. :)

  • edited December 2015 Answer ✓


    Like temp.content.updatePixels(); is not working due to not being of type PImage.

    Since field content is seen by Java as of type Object, you can't invoke method updatePixels() over it yet.
    Before doing that, you 1st need to tell Java that field content is actually of type PImage:
    temp.( (PImage) content ).updatePixels(); ~O)
    ( (PImage) temp.content ).updatePixels(); :\">

  • edited December 2015 Answer ✓

    If you prefer, you can also cache it in some variable: *-:)

    PImage img = (PImage) temp.content;
  • edited December 2015


    Hmm... interesting. Thanks for both the examples, but the temp.( (PImage) content ).updatePixels(); is giving me an error on the dot. " Error on "new ClassType ( )" " Thanks for your help aswell, it gives me a better insight of the language!

    [EDIT] Also, how do you show your code so neat?? :O

  • edited December 2015 Answer ✓

    ERROR: I've made a mistake about the placement of the (cast) operator after actually testing it: X_X
    ( (PImage) temp.content ).updatePixels();

    class Temp {
      Object content = createImage(100, 100, ARGB);
    void setup() {
      Temp temp = new Temp();
      ( (PImage) temp.content ).updatePixels();


  • @GoToLoop

    I must admit, that code works just like in my dreams. The last one was exactly what I was looking for, I can't thank you enough! Also, it works like a charm ;)

    Also, thanks alot for the link to format my codes! You solved to great issues of mine, can't thank you enough.

Sign In or Register to comment.