0

How can you create a Immutable Array??

Here's a piece of code from this lab I'm working on showing only one of the 3 classes: There are 3 classes, I won't write the codes for all of them: 1.Student.java class 2.Course.java class 3.CS210.java class(it has the main method) //Student.java class public class Student { private String Id; private String FirstName; private String LastName; private Course[] courses=new Course[1]; public Student(String Id,String FirstName,String LastName,Course[] course){ this.Id=Id; this.FirstName=FirstName; this.LastName=LastName; Course[] c=new Course[course.length]; c=course; this.courses=c; } public String getId(){ return Id; } public String getFirstName(){ return FirstName; } public String getLastName(){ return LastName; } public Course[] getCourseList(){ return courses; } } // the problem here is the ARRAY? Coz it's simple to make the other stuff in theclassimmutablebyfollowingtherules WHAT should I do to the method Above: public Course[] getCourseList?

8th Mar 2020, 4:44 AM
Nica STRAIT
Nica STRAIT - avatar
10 Respuestas
+ 2
return a copy from that array. this way the original array from the object wont recieve changes, even if the copy are being change. return courses.clone()
8th Mar 2020, 5:06 AM
Taste
Taste - avatar
+ 2
working with array inside class is sometimes a pain. because everything is a reference. there's no "real" immutable array. look at this sample, i just change the array that are used to create the object, and voila the instance variable alsp change https://code.sololearn.com/c14vTRPHYst7/?ref=app you can however create a clone of that array everytime it get passed. so it wont have a same reference. or, create a collection-like class to hold the array, and make sure the object immutable yourself edit: dont mind my second solution, java already have a way without you creating your own. https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collections.html#unmodifiableList(java.util.List)
8th Mar 2020, 5:50 AM
Taste
Taste - avatar
+ 1
~ swim ~ I tried that But my Dr JAVA Compiler keeps giving me this Error :cannot assign a value to final variable courses. ThankYou for your suggestion appreciate it.
8th Mar 2020, 5:09 AM
Nica STRAIT
Nica STRAIT - avatar
+ 1
Taste Thanks for the tip I'll try that
8th Mar 2020, 5:10 AM
Nica STRAIT
Nica STRAIT - avatar
+ 1
When you declare your instance variables, you don't need to initialize the array immediately. You would do it in the constructor anyway. It makes no sense to initialize an array with 1 element (in that case you could just use a simple variable). And you don't have to do all that magic with the c variable, just assign the argument directly. private Course[] courses; //inside the constructor: this.courses = course; What is your concern with immutability? The only way to assign courses now is via the constructor (when you create a new Student). So you cannot change the courses anyway.
8th Mar 2020, 5:11 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Tibor Santa thanks a lot.I'm New with the whole programming thing SO I appreciate ur help . Nevermind the Immutability.. Again Thanks Alot
8th Mar 2020, 5:19 AM
Nica STRAIT
Nica STRAIT - avatar
+ 1
~ swim ~ Thanks I'll try that
8th Mar 2020, 5:20 AM
Nica STRAIT
Nica STRAIT - avatar
0
Taste Thankyou so much I really appreciate ur help.Ur right working with an array within a class is a Pain! Thanks A lot
8th Mar 2020, 5:56 AM
Nica STRAIT
Nica STRAIT - avatar