Accessing variablss through a different class within the constructor of latter classes of an object
- by Haxed
In the code below, I've added two lines that print output. The first line prints junk as usual, but surprisingly the second one gives me a compilation error. Why?
class Student {
private String name;
public Student(String name){
this.name = name;
}
public String getName(){
return name;
}
}
class StudentServer {
public StudentServer(){
Student[] s = new Student[30];
s[0] = new Student("Nick");
// LINE 01: This compiles, although prints junk
System.out.println(s[0]);
// LINE 02: I get a error called cannot find symbol
System.out.println(s[0].getName());
}
public static void main(){
new StudentServer();
}
}
Many Thanks