Does Java have dynamic variables for class members?

Posted by Arvanem on Stack Overflow See other posts from Stack Overflow or by Arvanem
Published on 2010-04-07T23:04:35Z Indexed on 2010/04/07 23:23 UTC
Read the original article Hit count: 328

Filed under:
|
|

Hi folks,

I am wondering whether it is possible to make dynamic variables in Java. In other words, variables that change depending on my instructions.

FYI, I am making a trading program. A given merchant will have an array of items for sale for various prices.

The dynamism I am calling for comes in because each category of items for sale has its own properties. For example, a book item has two properties: int pages, and boolean hardCover. In contrast, a bookmark item has one property, String pattern.

Here are skeleton snippets of code so you can see what I am trying to do:

public class Merchants extends /* certain parent class */ {
        // only 10 items for sale to begin with
        Stock[] itemsForSale = new Stock[10]; 

        // Array holding Merchants
        public static Merchants[] merchantsArray = new Merchants[maxArrayLength];

        // method to fill array of stock goes here
}

and

public class Stock {
    int stockPrice;
    int stockQuantity;
    String stockType; // e.g. book and bookmark
    // Dynamic variables here, but they should only be invoked depending on stockType
    int pages;
    boolean hardCover;
    String pattern;
}

© Stack Overflow or respective owner

Related posts about java

Related posts about variables