Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7778385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:23:02+00:00 2026-06-01T18:23:02+00:00

For example, there is a class Warrior which have linked class Sword. In class

  • 0

For example, there is a class Warrior which have linked class Sword. In class Sword defined field: public static int hp = 100; which shows the health points consumed by this type of weapon. There is need for a few classes Warrior. I think I need to define in class Warrior the link Sword (only once) to be able to get static field hp. How can I link it properly?

class public Warrior{
    public String name;
    public Sword s = new Sword(); // ???
}
class public Sword{
    public static int hp = 100;

}

Will new Sword() create link to class each time new Warrior created?
Can I define Sword class as static inside another Weapon class? (There is a need for multiple classes like Sword)


Is following structure correct? Can outer class be static and hold inside another static?

class public Warrior{
        public String name;

        public int SwordHp = Weapon.Sword().hp;
        public int BowHp = Weapon.Sword().hp;

    }

(abstract?) public static class Weapon{
    public static class Sword{
        public static int hp = 100;

    }
    public static class  Bow{
        public static int hp = 90;

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:23:03+00:00Added an answer on June 1, 2026 at 6:23 pm

    For getting Static field hp you do not need to create

    public Sword s = new Sword();
    

    In warrior class. Static variables initialize on load. So you can access you hp anywhere without declaring in the specific classes by just using

    Sword.hp ;

    This do not have any impact on static variables.

     Sword s = new Sword()
    

    As i understand you Have a warrior and different weapons as currently Sword.Every weapon has health points hp and Every Warior has its own weapon and health points. If you use static fields then these health points will be shared among all warriors if 50 wariors then all will using just 100 points togather which i expect you do not want so you should use:

    class public Warrior{
        public String name;
        public Sword s = new Sword(); // ???
    }
    class public Sword{
        public  int hp = 100;
    
    }
    

    It will create a new sword with 100 health points every time a warrior is created and every warior will consume his own health points. Hope it will help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say, there is a following example: class Super { public int i = 3;
There is something bugging me about classes. For example class A { public: A()
Let's say I have a class Collection which holds a list of Items. public
I have two classes with a constants. For example there is a class called
/*I have defined Extension Methods for the TypeX like this*/ public static Int32 GetValueAsInt(this
I have this entity: public class MyEntity { [Key] public int Id { get;
Is there a way for taking type of a template class, for example //i
Is there a way in any browser to add/remove class names? For example, if
Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
I'm trying to implement a delegate class following Herb Sutter's Example . There is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.