• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Java How to make “Static” variable name to be dynamic

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose I have a class child


And I am using class child static variable age in class school


Value in age of child class could be any of these below depending on some logic:


How could i achieve this where should i apply that logic? Also it is mandatory for class school code to remain same.
 
Greenhorn
Posts: 16
jQuery AngularJS Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Vaibhav,

This honestly doesn't make a lot of sense to me. But it would be great if you can describe more about the problem you are trying to solve. But I've a few points to make

1. Why does a Child have a static field called age ? If each child object has an "age" property, it should be an instance variable.
2. I'm not too sure what you're doing in line 4 of the School class ? you don't need a age property for a school , and that too read from a static field of a Child.
3. Capitalize the first letter of your classes.

Thanks
Srikkanth
 
Marshal
Posts: 78656
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer about static fields is that you should avoid them as much as possible. As you have been told, there is only one copy of a static field per class, so making age static implies that all children are the same age.

You will occasionally find circumstances where static fields are appropriate, but I am sure this isn't one of them.
 
Grow your own food... or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic