Home > Back-end >  How to change the object's variable everytime an object created?
How to change the object's variable everytime an object created?

Time:05-30

My course registration project is supposed to have many methods as (add courses), (add tutors), etc. (add student()) is one of that methods and I am wondering how it is possible to change the variable (std) of the object every time it is created I don't know how every object may have the same variable

so, any suggestions I want to change the variable to be std1, std2, and like that

public static void addstudent(){
                   int n=0;

                   System.out.println("enter your desired no. of students");
                   int count=in.nextInt();
                   while (n!=count){
                       int st=0;
                       System.out.println("Enter your first name");
                       String first=in.next();
                       System.out.println("Enter your middle name");
                       String middle=in.next();
                       System.out.println("Enter your last name");
                       String last =in.next();
                       System.out.println("Enter your blood type");
                       String blood=in.next();
                       System.out.println("Enter your phone number");
                       int phone=in.nextInt();
                       System.out.println("Enter your nationality");
                       String national=in.next();
                       System.out.println("Enter your year of birth");
                       int yearofbirth=in.nextInt();
                       Student std=new Student(first,middle,last,blood,phone,national,yearofbirth);
                       System.out.println("A student added successfully");
                       n  ;
                       
                               }

And I wanna know if that what i am doing wrong or what?

And if every object is declared with the same variable it would clone the data of every created object to the new created one?

and thank you.

CodePudding user response:

I wanna know if that what i am doing wrong or what? And if every object is declared with the same variable it would clone the data of every created object to the new created one?

Each time you call new Student you are creating a new object. You then assign the reference to the object to the variable. Note that the variable doesn't contain the object. It only contains a reference (or pointer).

There is no "cloning" going on in your code. And no overwriting of the previous Student object.


However.

Your addstudent() method is apparently supposed to add a Student to something. It is not doing that. It is actually just creating Student objects and assigning their reference to a local variable. When the method returns, the local variable goes out of scope and the Student objects can no longer be accessed.

so, any suggestions I want to change the variable to be std1, std2, and like that.

You need to use a Student[] or a List<Student> or some other data structure to hold references to all of the students.

(Using std1, std2, etc variables is not the solution. Java doesn't support dynamic variables. And that probably would NOT be a good solution even in a language that does support them!)

  • Related