Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Sorry this is more of a Java question.  But I have a custom item called ItemGoldenEgg that extends Item.  I'm trying to create a new one and assign it to a static instance field.  Anyway, Eclipse warns me that ItemGoldenEgg is an Item and needs to be cast into ItemGoldenEgg.  I don't understand why I would ever have to cast something as itself, but I'm weak on some Java concepts..

 

Anyway here is the line of code (it complains if I don't have the cast in bold below):

        MagicBeans.itemGoldenEgg = (ItemGoldenEgg) new ItemGoldenEgg().setUnlocalizedName("golden_egg").setTextureName("magicbeans:golden_egg");

 

And the declaration for the instance itemGoldenEgg is in my main class and is:

    public static ItemGoldenEgg itemGoldenEgg;

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Sorry this is more of a Java question.  But I have a custom item called ItemGoldenEgg that extends Item.  I'm trying to create a new one and assign it to a static instance field.  Anyway, Eclipse warns me that ItemGoldenEgg is an Item and needs to be cast into ItemGoldenEgg.  I don't understand why I would ever have to cast something as itself, but I'm weak on some Java concepts..

 

Anyway here is the line of code (it complains if I don't have the cast in bold below):

        MagicBeans.itemGoldenEgg = (ItemGoldenEgg) new ItemGoldenEgg().setUnlocalizedName("golden_egg").setTextureName("magicbeans:golden_egg");

 

And the declaration for the instance itemGoldenEgg is in my main class and is:

    public static ItemGoldenEgg itemGoldenEgg;

 

Hello Jabelar!

Java does not want you to cast your ItemGoldenEgg to itself, it wants you to cast an Item to ItemGoldenEgg. What you are assigning to MagicBeans.itemGoldenEgg is not the object returned by your constructor, but rather the object returned by setTextureName(), which is the last method in that line, therefore what is returned by it is assigned to your variable. If you look at the definition for setTextureName(String par1Str), you can see that it returns an Item object. Java automatically casts object to their superclasses, like so:

Item randomItem = new ItemGoldenEgg(); 

but you need to manually cast it from a superclass to one of the inheriting classes, like so:

ItemGoldenEgg randomItem = (ItemGoldenEgg)new ItemGoldenEgg().setTextureName("TEXTURE");

 

Also, on a side note, you should probably just make the variable an Item, not an ItemGoldenEgg, and then cast it to your own class when you need to call your own methods.

Item itemGoldenEgg = new ItemGoldenEgg().setUnlocalizedName("golden_egg").setTextureName("magicbeans:golden_egg");
(ItemGoldenEgg)itemGoldenEgg.hatchEgg();

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.