Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use table inside a class java [closed]

Tags:

java

Please I need a small help concerning a code java, whenever I came to initialise a table as a attribute in a class I didn't find the definition logic, since no [] setted there so I'm kind of curious to know with an example of class which work with a table attribute & should I initialise the table in the default constructor by null? So, I did my best but I can't understand this code I had wrote it & of course there will be many errors as I think:

public class ClasseSMI {
private String _filiereName;
private String[] _etudiantsList;
public ClasseSMI()
{
    this._filiereName ="jjjjj";
    this._etudiantsList = null;
}

public String toString() {
    return _filiereName + "  " + _etudiantsList;
}
    public static void main(String[] args) {
        ClasseSMI smi = new ClasseSMI();
        System.out.println(smi);
    }

}

so any one can help with example please ?

Thanks in advance !

like image 961
lastrev83 Avatar asked Sep 18 '26 08:09

lastrev83


1 Answers

You just began to learn Java. There are so many way to do what you want to achieve. Null initialization is only one way to initialize reference to array. Default but not best one. Here is what you, probably, want:

public class ClasseSMI {
private String _filiereName;
private String[] _etudiantsList;
public ClasseSMI()
{
    this._filiereName ="Alex";
    this._etudiantsList = new String[]{"Nick","Mark","Nickole"};
}

public String toString() {
    String result=_filiereName+":";
    for(String etudiant:_etudiantsList){
        result+= " "+ etudiant;
    }
    return result;
    //return _filiereName + "  " + _etudiantsList;
}
    public static void main(String[] args) {
        ClasseSMI smi = new ClasseSMI();
        System.out.println(smi);
    }

}

It returns

Alex: Nick Mark Nickole

If you use your return it will use default toString() for array and it will look like this:

Alex [Ljava.lang.String;@17dfafd1

like image 80
Alex Avatar answered Sep 19 '26 21:09

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!