• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

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: 77245
371
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic