Jump to content

[Any Version] Nested classes: where and when


ktreus

Recommended Posts

Greetings. My nick is ktreus and i want to know where we can use nested classes and the situations we can apply them in modding, because im using to make everything in my mod and all seems suprisely working well, but my mod is a big project (almost a divine rpg) and idk if this will imply in future.

Link to comment
Share on other sites

When you use nested classes depends on your code style. There's not really a specific area where you need to use them, they mostly behave just like normal classes.

 

One common use is nesting the IMessageHandler in the corresponding IMessage class to keep the message and its handler together.

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

3 minutes ago, Choonster said:

One common use is nesting the IMessageHandler in the corresponding IMessage class to keep the message and its handler together.

While true, I recommend against it due to the potential to confuse the scope ("my packet handler runs, but it doesn't recieve the data, but I know I'm sending it!") as the modder tried to reference the packet data not from the object passed, but through container scope, even through the objects were different instances.   Keeping them in separate files helps the fledgling modder. 

 

I use nested classes for things like data wrappers, where I need to bundle two items together for a sorting function, or similar, but that that wrapper is never used outside that scope. 

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

8 minutes ago, Draco18s said:

While true, I recommend against it due to the potential to confuse the scope ("my packet handler runs, but it doesn't recieve the data, but I know I'm sending it!") as the modder tried to reference the packet data not from the object passed, but through container scope, even through the objects were different instances.   Keeping them in separate files helps the fledgling modder. 

 

If the handler is a static nested class (which it should be), attempting to access instance fields of the message results in a compilation error.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

13 minutes ago, Choonster said:

 

If the handler is a static nested class (which it should be), attempting to access instance fields of the message results in a compilation error.

Oh I agree. Just that some new folks don't know that and seem to think that because the code is in the same file it's fair game to dip things that won't work. Like making the message and message handler the same class...

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

29 minutes ago, diesieben07 said:

There is no "static nested class". Nested classes are static. Non-static "nested" classes are called inner classes.

 

Java's tutorials do use the term "static nested classes".

Edited by Choonster

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

I nest my common proxy class inside my abstract main mod class. Not only can my abstract mod declare a proxy for every mod, but the proxy class has visibility to fields and methods in the abstract mod (and client proxies can call inherited methods that exploit that visibility). Since a proxy acts on behalf of a mod, it seemed to make sense to me to grant that visibility.

 

Similarly, when I define a tile entity class, I usually nest it inside the block class that it supports.

 

In vanilla Minecraft, I see loads of enum classes defined within other classes that use them to define variants.

 

However, don't take Minecraft and modder's hacks as a guide to good programming discipline. If you're curious about learning general Java and programming, then look to a more general Java forum. Hackers like me might show you bad habits that you'll need to unlearn someday.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

3 hours ago, jeffryfisher said:

In vanilla Minecraft, I see loads of enum classes defined within other classes that use them to define variants.

This makes sense...in vanilla.

If you're making a mod and want to expose your block properties, then the enum should be exposed (but not necessarily the block) in an API.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.