More on Classes and Objects
Objects as Method Parameters
- Remember, objects are created with the new operator. The name we use is a reference variable
- When an object is passed into a method, the reference variable is copied into the method's local parameter [just like with arrays] -- method parameter becomes a reference to the original object
- Bottom line: When an object is passed into a method [by its reference variable], the method has access to the original object. Changes to the object [from inside the method] will affect the original
Class Variables and Methods
The modifier static can be used on variables and on methods- Variables
- A static variable is shared by all instances of a class. Only one variable created for the class.
- Instance variable [not static] -- each object [i.e. each instance of a class] gets its own copy of such a variable
- Methods
- A regular method [instance method] can only be called by an object [an instance of the class]
- A static method [class method] can be called without creating instances of a class. Called through class name or object name -- but a better practice to call through the class name [to help remind that they are static]. Example: Math.round[x]
- access
- Static variables can be accessed from both instance methods or static methods.
- Instance variables can not be accessed from static methods [since instance variables only exist when an object exists]. Instance variables can be accessed from instance methods
Example
class Student { private int testGrade; // instance variable [non-static] private static int numStudents = 0; // static variable [class variable] private final static int pointsPossible = 100; // class constant public Student[] { testGrade = 0; } public void setGrade[int gr] { testGrade = gr; } public int getGrade[] { return testGrade; } public static void incrementNumStudents[] { numStudents++; } public static int getNumStudents[] { return numStudents; } }In this sample code:
- testGrade is an intance variable. Each object of type Student will have its own copy of testGrade
- numStudents is a class varaible [static]. There is only one variable shared by the whole class. The variable's value can be changed, but changes are seen by all objects
- pointsPossible is a class constant. There is only one variable [because of static], and its value cannot be changed]
- setGrade and getGrade are instance methods. They must be called through individual objects
- incrementNumStudents and getNumeStudents are static methods. They cannot access instance varaibles of the class, but they can be called through the class name, regardless of whether any objects have been created
The Keyword this
- In Java, the keyword this is a reference varibable to the current calling object [from inside an instance method]. Once inside the instance method, this acts as the reference name for the calling object that you are in.
- In Java, this can also be used to call one constructor from another in a class, for the current calling object. Use it like the function name in the
call. Example:
public Date[int m, int d, int y] // constructor with 3 params { month = m; day = d; year = y; } public Date[int m, int d] // constructor with 2 params { this[m, d, 0]; // calls constructor with 3 params }
Fraction class example
Here is a small class example -- it is a type for implementing Fraction objects.- class Fraction
Arrays of Objects
Creating an array of objects is a little trickier than an array of a primitive type.- Create an array using similar syntax to primitive types, but use the class name instead of the
primitive type name:
Student[] list = new Student[10];
This only creates an array of reference variables -- references for type Student
- Create the individual objects with new, and attach to the reference variables [the array positions]. This can be done separately:
list[0] = new Student[]; list[1] = new Student[];
but it's easier with a loop [as long as you are using the same constructor for each object]:for [int i = 0; i < list.length; i++] list[i] = new Student[];
Each list[i] is the reference to an object now.
More class examples in Deitel Chapter 8
What variables are shared by all objects of a class?
A static variable is shared by all instances of a class. Only one variable created for the class.
Which variable is a variable whose single copy is shared by all the objects?
Static Variables: When a variable is declared as static, then a single copy of the variable is created and shared among all objects at a class level.
Which variable value is common to all objects?
Static variables are common to all objects.
Which variables are shared variables in Java?
All public classes are shared, all variables can be shared if they accessible through public methods, etc. they can be shared as protected and private too as long as the calling method has access to them.