- package com.javastepbystep;
- class A
- {
-
- static int method1(int i)
- {
- return method2(i *= 11);
- }
-
- static int method2(int i)
- {
- return method3(i /= 11);
- }
-
- static int method3(int i)
- {
- return method4(i -= 11);
- }
-
- static int method4(int i)
- {
- return i += 11;
- }
- public static void main(String [] args)
- {
- System.out.println(method1(11));
- }
-
- }
Ans: 11
You can declare object of class Box as follows:
Box box;
Normally we say that box is an object of type Box. This is correct in C++ but this is not fully correct in Java. Because box is simply a variable that can hold reference to an object of type Box.
This is like pointer to a structure in C/C++. The reference box will store garbage or null reference unless we assign reference of some object of class Box to it.
After the above declaration variable box will contain garbage or null reference as shown below:
This indicates that variable box is not pointing to any object.
If the variable box is defined in a method or block it will contain garbage, as local variables are not initialized by default. To make sure that it contains null unless some valid reference (conceptually same as pointer of C/C++) is assigned to it, you must declare and initialize the box as follows:
Box box = null;
If the above declaration of box is outside any method/block then it is an instance variable and there is no need not initialize it with null as an instance/reference variables are always initialized with null.