2

I know what static is.. Globally.

So I was looking through a code, to get better in coding myself. I'm looking through the Minecraft source code, and for those who are interested to look there its in the files "TileEntity.java" and "EntityList.java". It is definitely not necessary to look over there, because its just a manner of programming.

So, we have just your regular class with a method:

public class EntityList{
    public static void addMapping( /* variables that dont matter */ ){
        //Call other methods, also unimportant
    }
}

After that there is a class that imported EntityList and does this:

import the.path.to.EntityList;
public class TileEntity{
    static{
        addMapping( /* vars */ );
        addMapping( /* vars */ );
    }
}

Now I'm wondering: How does this work? Please let me know if you need to know more background of the code, but I cannot redistribute the file due to copyright and stuff. Then you have to decompile Minecraft if you have it yourself.

4
  • docs.oracle.com/javase/tutorial/java/package/usepkgs.html Commented May 18, 2013 at 20:18
  • Don't just say "it doesn't work". Show us what you tried, and paste the exact error message you get. Commented May 18, 2013 at 20:26
  • If you have two different questions you should post them as two different questions. Commented May 18, 2013 at 20:27
  • Thank you JB Nizet, I take that in mind for the next time. And of course, thank you ellak for the comment. I had it figured out afterwards how to import a class, it was a silly spelling error that I missed. Now lets wait for the first question :) And I forgot Brian, thank you. That helped me quite a lot for future reference Commented May 18, 2013 at 20:40

1 Answer 1

3

We can't see the real code, but my guess is that it contains a static import:

import static the.path.to.EntityList.addMapping;

or

import static the.path.to.EntityList.*;

A static import allows referring to a static field or method of a class without having to type the name of the class.

See http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html for more details.

Sign up to request clarification or add additional context in comments.

2 Comments

That makes total sense. But now I noticed, that it actually isnt imported by the same class. It doesnt inherit anything either, so now Im having my doubts.. So, is it possible that this does has anything to due with the packaging? So if the file structure is like this: import static path.to.EntityList.addMapping; -> new folder -> static{ addMapping( /* something */ ); }. And what you already have posted, is very helpfull. Thank you very much for that!
I don't understand what you're saying. Post code in your question. If it's proprietary code, then make your own classes with similar code and a similar structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.