the code is :
interface I {
int i = 0;
void display();
}
class A implements I {
I i1;
public static void main(String[] args) {
A a = new A();
a.display();
}
public void display() {
System.out.println(i1); //1
System.out.println(i1.i); //2
}
}
The output of the code is
null
0
But when the address of the i
is null
, then in the 2nd i1.i
how does it return a value ?
How can a null reference be used to point to a variable ?
Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface.
null return means a return which indicates that no transaction was made by the registered person during the tax period and no amount of tax is to be paid or refunded. Sample 1.
Returning interface allows a member function to return reference of any implemented class. It gives flexibility to program to an interface and also is helpful in implementation of factory and abstract factory design pattern.
Fields declared in interfaces are implicitly static
.
Every field declaration in the body of an interface is implicitly
public
,static
, andfinal
. It is permitted to redundantly specify any or all of these modifiers for such fields.
This
i1.i
is a static
field access expression. It relies on the type of i1
, not its value. It is exactly equivalent to
I.i // where I is the name of your interface
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With