Array of Objects - Java Tutorial - Java With Us(1)
Array of Objects - Java Tutorial - Java With Us(1)
Classes and Objects The above statement creates the array which can hold references to seven Student objects. It doesn't
Introduction to object oriented create the Student objects themselves. They have to be created separately using the constructor of the
programming Student class. The studentArray contains seven memory spaces in which the address of seven Student
The constituents of a Class objects may be stored. If we try to access the Student objects even before creating them, run time errors
would occur. For instance, the following statement throws a NullPointerException during runtime which
Creating objects and calling
indicates that studentArray[0] isn't yet pointing to a Student object.
methods
Get and Set Methods studentArray[0].marks = 100;
Default constructor provided by
the compiler The Student objects have to be instantiated using the constructor of the Student class and their
references should be assigned to the array elements in the following way.
Access Specfiers
Scope and lifetime of Variables
studentArray[0] = new Student();
Call by value and Call by Reference
In this way, we create the other Student objects also. If each of the Student objects have to be created
A few more topics using a different constructor, we use a statement similar to the above several times. However, in this
Casting particular case, we may use a for loop since all Student objects are created with the same default
constructor.
Class as a reference data type
Constants or literals for ( int i=0; i<studentArray.length; i++) {
Final variables studentArray[i]=new Student();
Increment and decrement }
operators
The above for loop creates seven Student objects and assigns their reference to the array elements.
Manipulating Strings Now, a statement like the following would be valid.
Operators
Overloading constructors and studentArray[0].marks=100;
methods
Static methods and variables Enhanced for loops find a better application here as we not only get the Student object but also we are
capable of modifying it. This is because of the fact that Student is a reference type. Therefore the
The Java API
variable in the header of the enhanced for loop would be storing a reference to the Student object and
The Math class not a copy of the Student object which was the case when primitive type variables like int were used as
this keyword array elements.
Wrapper classes
for ( Student x : studentArray ) {
x.marks = s.nextInt(); // s is a Scanner object
Control Structures
}
Control Statements
Repetition statements Recall that we were not able to assign to the array elements in a similar way when the array was of type
Nested loops int.
Formulating algorithms
Branching Statements Moreover, in the case of array of objects, when we pass an array element to a method, the object is
susceptible to changes. This is because the element being passed is also a reference type item. This
differs from the situation when we have an int array. Following illustrates this concept.
Arrays
Arrays introduction public static void main(String[] args) {
Processing arrays using 1oops Student[] studentArray = new Student[7];
Searching and sorting arrays studentArray[0] = new Student();
studentArray[0].marks = 99;
Array of objects
System.out.println(studentArray[0].marks); // prints 99
Multi dimensional arrays modify(studentArray[0]);
Taking input as command line System.out.println(studentArray[0].marks); // prints 100 and not 99
arguments // code
}
Using ellipsis to accept variable
number of arguments public static void modify(Student s) {
s.marks = 100;
}
Inheritance
Inheritance introduction
Compare the output with the one when the array was of type int[].
Relation between a super class
and sub class Processing an array of objects is much similar to the processing of an array of primitive type. the only
Final classes and methods thing to be kept in mind is the possibility of NullPointerException being thrown during run time and also
The protected access specifier remembering that the array elements themselves are reference types, which brings subtle differences
Class Object from the case when they are passed as parameters. Moreover, an enhanced for loop may be used to
initialise the array elements.
Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions
Java With Us
4,3 rb suka
Sukai Halaman
Privacy Policy