Dc Redwing
Dc Redwing

Reputation: 1791

How to create data object dynamically in java?

I am studying data object in Java

I have question for creating the data object dynamically.

For example ,

we have...

public class tasks {
private int vmnumber; 
private int tasknumber;
private String status;
public tasks(int vmnumber , int tasknumber , String status) {
this.vmnumber = vmnumber;
this.tasknumber = tasknumber;
this.status = status; }

and there are some getvmnumber gettasknumber , getstatus , and some set functions for

what I understand about creating data object is we have to initialize each time.

for example , in the main file ,

public class task{
public static void main(String [] args){
task t = null , t2 = null;

t = new task();
t.tasknumber = 3;
t.vmnumber = 4;
t.status = "Start";

t2 = new task();
t.tasknumber = 2;
t.vmnumber = 1;
t.status = "Wait";
}

however, i would like to how we can create data object dynamically because program possibly get the information of tasks on real time.(then we can't manually create the data object, we need to something which can create the data object dynamically...)

Second, I would like to know how to get the data from data object.

For example , if we want to find all the information of task number 3 , what should i do ? lets say , we have task1, task2, task3 data object and we want to see the all information of task1. then what should i do ?

thanks

Upvotes: 0

Views: 3817

Answers (1)

jeromedt
jeromedt

Reputation: 199

There are few points to discuss, from your question.

I guess you want to create new tasks, which is maybe a request from the user interace of your application, or a webservice, a batch...

Well, you already know how to create object : with the new keyword. Depending on the original request, your main function may have to create multiple instances of the same class, "Task".

More, when you instantiate the class "Task", you would never want to assign directly values to the properties of it.

So, instead of coding t.tasknumber = 3, you should code : t.setTaskNumber(3)

Also, you should rename the properties of your class to reflect the JavaBeans conventions : - private int taskNumber instead of tasknumber

Of course, it is only a convention, and it is not mandatory in your program. But it helps generating getters/setters, and, well, it is a convention :-)

To retrieve "information" within your created tasks, you only have to call the getters : - myTask.getTaskNumber()

Hope this helps you a little bit.

Upvotes: 1

Related Questions