Java - University of Technology - 21

Exercise

1. Are the following statements true or false?

a) When an object of a subclass is instantiated, the constructor of the parent class must be called explicitly.

b) if a class has constructor declarations, the compiler will not create a default constructor for that class.

Maybe you are interested!

c) The child class inherits the constructor of the parent class. When instantiating a child class object, the constructor of the parent class is always called automatically to initialize the inherited part.

2. What is the new keyword used for? Explain what happens when you use this keyword in an application.

Java - University of Technology - 21

3. What is a default constructor? How are the instance variables of an object initialized if the class does not have any constructor written by the programmer.

4. Find the compilation error if any in the constructors in the following implementation of class SonOfBoo.


5. Given the implementation of class Foo in the left column, if one of the lines of code in the right column is added to position A, which line will cause an object to be lost and reclaimed by the garbage collector at any time?

Chapter 10. Class members and entity members


We know that for instance variables, each object has its own copy of each variable. For example, if the Cow class is declared with the instance variable name, then each Cow object has its own name variable located in the memory allocated to that object. Most of the methods we have seen in the examples have operations that are affected by the values ​​of instance variables. In other words, their behavior depends on each specific object. When we call methods, we also have to call specific objects. In short, these are methods that belong to objects.

If we want some data of a class to be shared among all objects of a class, and the methods of the class to work independently of the objects of that class, then the solution is class variables and class methods.


10.1. CLASS VARIABLES


Sometimes, we want a class to have variables that are common to all objects in that class. We call these common variables class variables, or class variables for short. They are not associated with any particular object, but only with the class of objects. They are shared by all objects in that class. To distinguish between instance variables and class variables when declaring in a class definition, we use the keyword static for class variables. Because of that keyword, class variables are often called static variables.

For example, in Figure 10.1, in addition to the instance variable name, the Cow class also has a class variable numOfCows that records the number of Cow objects that have been created. Each Cow object has its own name variable, but there is only one copy of numOfCows that is shared by all Cow objects. numOfCows is initialized to 0, and each time a Cow object is created, this variable is incremented by 1 (in the constructor for that object) to record that a new instance of the Cow class has been created.

@ (

" &


: " 9 / "

@2:

" ! @

" R'@ ( # 3&

/ "


@ ( "

# &


R'@ ( ))&

W . 0 L

@ 5: *

) % GCI Q d

- , @


7 7 8@ ( J8) R'@ ( )8 78 &



@ (IA

"EF "

9 ,

9 (

9 # (

@ ( 2 # ( @ ( &

@ ( 1 # ( @ ( &


Figure 10.1: Class variable - static variable.


From outside the class, we can use the class name to access the static variable. For example, use Cow.numOfCows to access numOfCows:



10.2. CLASS METHODS


Consider the example in Figure 10.1 again. Suppose we want numOfCows to be private, so that no one can modify it from outside the Cow class. But we still want to be able to read its value from outside (programs that use Cow might want to know how many Cow objects have been created), so we add a method, say getCount(), that returns the value of the variable.

public int getCount() { return numOfCows;

}

Like the methods we're used to, to call getCount(), we need a reference of type Cow and invoke that method on a Cow object. Needing a cow to know how many there are? Doesn't sound very natural. Besides, calling getCount() on any Cow object is essentially the same, since getCount() doesn't use any specific characteristics or data of the

each Cow object (it does not access any instance variables). Furthermore, when no Cow object has been created yet, getCount() cannot be called!

The getCount() method should not depend on specific Cow objects like that. To solve this problem, we can make getCount() a class method, often called a class method – or static method – so that it can exist independently of the objects and can be called directly from the class without needing an object reference. We use the static keyword when declaring a class method:

public static int getCount() { return numOfCows;

}

The normal methods we know, except main(), are called instance methods – or non-static methods. These methods depend on each object and must be called from the object.

Figure 10.2 is a modification of the example in Figure 10.1. It adds the static method getCount() and demonstrates calling it from the class name as well as from an object reference. This time, we can query the number of Cows even when no Cow objects have been created. Note that getCount() can be called from the class name as well as from a reference of type Cow.

public cl ss Cow {

first of all

pri te st tic int numOfCows #3;


public Cow( tring te me) { n me # te me; numOfCows));

}


public static int getCount() { return numOfCows;

}


public tring get me() { return n me;


2 3 4 @2

" ! A / " : " 9 L


@ "

IQ . -

9 "

#

}

}


public cl ss CountCows {

public st tic oi m in( tringEF rgs) {

stem7out7println(Cow7getCount()); Cow c2 # new Cow();

stem7out7println(Cow7getCount()); Cow c1 # new Cow();

stem7out7println(c17getCount());

}


1 / - @2


< 1 /

}


Figure 10.2. Class method.


The object-independent nature of static methods is the reason we always declare the main() method with the static keyword. main() is called at the start of the program - when no objects have been created - so it must be allowed to run without any objects attached.


10.3. LIMITATIONS OF CLASS METHOD


That independence is both an advantage and a limitation for the operation of class methods.

Since they are not tied to any object, static methods of a class run without knowing anything about any particular object of that class. As shown in Figure 10.2, getCount() runs even if no Cow object exists. Even if getCount() is called from a reference to c2, getCount() still knows nothing about the Cow object that c2 is referring to. The compiler then uses only the type of c2 to determine which class's getCount() should be run; it does not care which object c2 is referring to. Cow.getCount() or c2.getCount() are just two ways of calling the method, and either way, getCount() is still a static method.



Figure 10.3: Class method cannot access instance variable.


If an instance variable is used in a class method, the compiler will not understand which object's instance variable we are talking about, regardless of whether there are 10 objects of that class on the heap or only one. For example, the program in Figure 10.3 fails to compile because the main() method tries to access the variable name. Since main() is a static method, the compiler does not understand which object's instance variable name is being referred to by main(). The error message reads: the instance variable name cannot be called from a static context. It is easy to see that the this reference cannot be used in a class method either, because it does not understand which object 'this' is.

The knock-on effect of static methods not being able to use instance variables is that they also cannot call instance methods (normal methods) of that class. Instance methods are allowed to use instance variables, calling instance methods means indirectly using instance variables.


Figure 10.4: Class methods cannot call instance methods.


The example in Figure 10.4 also has the same error as the compilation error in Figure 10.3.

Comment


Agree Privacy Policy *