0

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

2
  • 1
    you're creating a data object dynamically right there... Commented Nov 12, 2012 at 20:50
  • What do you mean by creating it "dynamically"? In what way would it by dynamic? And to retrieve the information of task1, you have the getters (that you said you have) that you can use to read its variables. Am I missing something here? Commented Nov 12, 2012 at 20:53

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

We create getters and setters to ensure encapsulation. This is a central concept of OOP (Object Oriented Programming). Only your class (or maybe the classes that inherit from it) may directly access its properties. Consider this example : you have a class Person, which holds a "age" property. If any code could change the value of myPerson.age = -20, imagine the mess :-) With a setter, myPerson.set(int age), you may check the value before assign it to the instance "myPerson".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.