When you initiate the Student1 array, you are creating a new Student1 (array) object, you are not creating any Student1 objects, and since all array objects (or primitives) are assigned there default value, each Student1 object in the Student1 array object is set to null. Try:
Hi Joe, Welcome to JavaRanch� A quick administrative note, it is generally a lot easier if you place any sample code in UBB [code] [/code] tags. It makes reading the code much easier. Take a look hereto learn how to use UBB�
In addition to Jason�s comment, there is another "problem" in your code that should be pointed out so you do not confuse yourself about something. Looking at your code with line numbers for easy referencing:
The code on lines 3-6 is not actually a constructor, as you indicate in your System out comment; based on the way you have it defined, it is a method. Constructors must be named the same as the class and do not have a return type, not even void. I think you just made a typo since you have similarly named classes, Student and Student1. To be a constructor, you need to change line so the name matches the class, and remove the return type:
This will now output the comment when you construct an instance of Student1 using the syntax Jason gave you.
Previously, a Student1 object could still be constructed since by default, all objects have a no argument constructor such as this:
If we change lines 3-6 to be a constructor instead of a method named "Student", it will make line 14 syntactically incorrect if your intent was to call the constructor since you do not call constructors with dot-notation. As Jason indicates, you need to use the keyword new.
If you then wanted to call a method on the object, you use the dot-notation on any methods you have defined. Since having a method named Student inside the class Student1 which is used in the Class Student is rather confusing, let�s instead add a method named sayHello to the Student1 class so you can see how to call a method. So if we take all the above along with Jason�s comments, we would have this:
This should run and give you the output:
Call to constructor complete. sayHello method called
As you continue to read about Array�s, you should learn how to iterate through the entire array and place an object in each position.
Hope that helps
Joined: Jun 08, 2004
Wow! I can't thank you both enough! And thanks for clearing up about methods and constructors.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com