Jump to content
  • Advertisement
Sign in to follow this  
dxFoo

Need help with embedded classes in Java

This topic is 4777 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In C++, I was able to do the following (which is now Java syntax)...
class Common
{     
    public String name;

    class Weapon
    {
        public static boolean Sword;
    }
}
PC.Weapons.Sword = true; PC (player) was a class, and Weapons was an embedded class. Sword is just a static boolean whether the player had a sword or not. I noticed that doing the same in Java is different - inner classes cannot have static variables. How can I achieve a similar result?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
class Common
{
public String name;

static class Weapon
{
public static boolean Sword;
}
}

Share this post


Link to post
Share on other sites
I sought, but didn't find, other than everything having to be static.


import java.lang.*;

class Main
{
public static void main(String args[])
{
Common PC = new Common();
}
}

class Common
{
public static class Weapon
{
public static boolean Sword;
}
}



PC can't access class Weapon. Common can, but that isn't desired, though.

Share this post


Link to post
Share on other sites
AP, before you posted, I tried exactly what you did. I'm still not able to access Weapon. Error message is "unexpected type" at PC.Weapon.Sword = true; I assume that instantiated objects can't use static variables/methods/classes.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Wouldn't something simple like this suffice?


class Common
{

public String name;


class CommonWeapon
{
public boolean Sword;
}

public CommonWeapon Weapon = new Weapon();


}

Share this post


Link to post
Share on other sites
Hmm, I never thought about that. After changing your line to...

public CommonWeapon Weapon = new CommonWeapon();

... it compiled. Thank you very much!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Whoops! Damn copy and paste... :P

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!