Are Java dynamic variables for class members?

I am wondering if dynamic variables can be created in Java. In other words, variables that change depending on my instructions.

EDIT To paraphrase my question, I mean variables for a class that change type depending on the given variable (stockType, for those who read).

FYI, I'm doing a trading program. This trader will have many items for sale at different prices.

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

Here are the skeleton code snippets so you can see what I'm 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; // eg book and bookmark // Dynamic variables here, but they should only be invoked depending on stockType int pages; boolean hardCover; String pattern; } 
+4
source share
6 answers

Java does not allow dynamic variables. Instead, you should use object-oriented design methods.

In this case, you must define an abstract class Stock with common methods and members and extend this class to types: Book , Bookmark > etc. See below for an example.

The benefits of using an abstract class for stocks (which none of the other answers have yet been shown) is that the "Stock" element cannot exist by itself. It makes no sense to have 5 stocks on a sellerโ€™s shelf just as it makes sense to have 5 books on a sellerโ€™s shelf.

 public abstract class Stock { private int stockPrice; private int stockQuantity; // Implement getters and setters for stockPrice and stockQuantity. } public class Book extends Stock { // Since I'm extending Stock, I don't have to redefine price or quantity private int pages; private boolean hardCover; // Implement getters and setters for pages and hardCover. } public class Bookmark extends Stock { private String pattern; // Implement getters and setters for pattern. } 

Please note that in order to determine what type of object you deal with later, if you absolutely need to, you will use checks such as:

 if (myStock instanceof Book) { ... } 
+3
source

Stock subclass for each stock type:

 public class Stock { private int price; private int quantity; } public class StockBook extends Stock { private int pages; private boolean hardCover; } public class StockBookmark extends Stock { private String pattern; } 

Or use Map for different types of attributes:

 public class Stock { private int price; private int quantity; private String classification; // eg book and bookmark private Map properties = new HashMap(); } 
+5
source

You might want to use polymorphism.

 public class Stock { int stockPrice; int stockQuantity; int stockType; } public class Book extends Stock{ int pages; boolean hardcover; } public class Bookmark extends Stock { String pattern; } 
+5
source

NO and for good reason.

In any case, to do what you need, instead of having the type of StockType, subclasses in the different types that you want.

Give us examples of what you want to do with dynamic variables, and we can show you how to do it.

Also do not use arrays. They are of fixed length and only really should be similar to C ++. Use List instead.

Replace Stock[] itemsForSale = new Stock[10]; on List<Stock> itemsForSale = new ArrayList<Stock>(); and read about List

+1
source

I'm not sure what you want for sure, but it looks like State design-pattern might be something you can check.

0
source

Here you can use the factory design template . The reason is that based on the contentType type, you need a specific / dynamic object that the factory will provide you with.

0
source

Source: https://habr.com/ru/post/1306309/


All Articles