3

so I've recently started learning java but I'm having a little problem:

OrcMage.health -= 80;

but if you saw all of my code, you'd see that I use the OrcMage class a lot, and I still have a lot to go. Would it be possible to create something like a namespace so I could type health -= 80;instead?

  • 2
    You must be a C++ developer. Better to learn how to write Java in its proper idiom than try to write C++ in Java. I hope you don't have a class with lots of public static variables. Bad design. – duffymo Dec 31 '15 at 18:33
  • 1
    Is this all from within the OrcMage class, or from outside? – Andrew Williamson Dec 31 '15 at 18:33
  • if `health` is a `static` property , you can use static import to access it directly. – Ramanlfc Dec 31 '15 at 18:34
  • You do realise that unless you're _guaranteeing_ that you only ever have one `OrcMage` (and where would the fun be in that?) doing this is a really bad idea? Astoundingly bad. Indeed, it is good style to have all non-constant variables having no more than package visibility for all non-POJO classes (POJOs are classes that are treated very much like open C++ structs in how they're used). – Donal Fellows Dec 31 '15 at 19:10

1 Answers1

1

Declare health as static then use

import static OrcMage.health;
Mohammad Salem
  • 1,113
  • 9
  • 15