• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
AngularJS Chrome jQuery
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!