• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About Interface constants

 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this program

public class InterfaceInit1 {

public static void main(String[] args) {
Base1 b = new Base1();
}
}

interface ImplementMe {
static final int ONE = Base1.one();
static final int TWO = Base1.two();
}

class Base1 implements ImplementMe {
static int one(){
System.out.println("One");
return 1;
}

static int two() {
System.out.println("Two");
return 2;
}
}

This program doesnt display any output.
Now i just change the program to be like this:

public class InterfaceInit1 {

public static void main(String[] args) {
Base1 b = new Base1();
}
}

interface ImplementMe {
static final int ONE = Base1.one();
static final int TWO = Base1.two();
}

class Base1 implements ImplementMe {
static final int HERE = base1.one();
static int one(){
System.out.println("One");
return 1;
}

static int two() {
System.out.println("Two");
return 2;
}
}

Here it displays the output as "One", which i guess means that the constant member of class is initialized here during run time
I just wanted to know why not the same for interface constants? after all a class is implementing it, so its members should get initialized.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Animesh ,

A class is loaded by JVM in the following cases :
1] when you instantiate a class .
2] when any static member is get called - even no object is get created .
3] reflection - Class.forName("java.util.Date()");

So in your first code , Base1 class has two static methods but no static vatriable . so when you create an object , no method is getting call .

But in your second case , Base1 class has one static variable & this will be initialized at the time of class loading . so the method one() get called ( just because initialization of static variable )

I hope it is right & useful .
bye .
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rathi for that explanation,
Now tell me one thing, if the class gets loaded, wont the interface which the class implements too get loaded.

I was playing with that program, now i changed my program a bit:

public class InterfaceInit1 {

public static void main(String[] args) {
Base1 b = new Base1();
}
}

interface ImplementMe {
static final int ONE = Base1.one();
static final int TWO = Base1.two();
}

class Base1 implements ImplementMe {
static final int HERE = ONE;
static int one(){
System.out.println("One");
return 1;
}

static int two() {
System.out.println("Two");
return 2;
}
}

Now the output is displayed as:
One
Two

Which i think implies that the other constant of interface too gets initialized along with the first one
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although I am also a bit confuse but my explanation is giving your answer .
As I mantioned in 2nd point :
classs loaded by JVM whenever you access its static member .
So now you are accessing a static member of interface , so it will be loaded now and all static variable will get initialize now ......
I hope it is clear & also most important is right .
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing in addition :
if your interface ( ImplementMe ) will be a class , then its variable will also be initialized .



output will be :
One
Two
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah thats fine rathi,
But we need to sum up the important points regarding this otherwise it becomes really confusing.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Animesh ,
but it is sure that interface static variable are not initialized at the time of loading a class that has implemented that interface , but why ??
please help us ?
thanks a lot .
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, i have summarised it as:

1) Inteface gets initalized when its first accessed.
2) But abstract class gets constructed as the class extending it is constructed.

In other words:
super interfaces make use of lazy initialization and the super classes make use of early initialization

Well about the super classes early initialization i have the following program which demonstrates it:

public class InterfaceInit1 {
InterfaceInit1() {
}

public static void main(String[] args) {
Base1 b = new Base1();
}
}



class Base1 extends absTr {
static int one(){
System.out.println("One");
return 1;
}

static int two() {
System.out.println("Two");
return 2;
}
}


abstract class absTr {
static final int i = Base1.one();
static final int j = Base1.two();
}

The output is displayed as:
One
Two

Do let me know if i am making sense?

Thanks
Ani
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Java Language Specifications, Section 12.4.1:


Initialization of a class consists of executing its static initializers and the initializers for static fields declared in the class. Initialization of an interface consists of executing the initializers for fields declared in the interface.

Before a class is initialized, its direct superclass must be initialized, but interfaces implemented by the class need not be initialized. Similarly, the superinterfaces of an interface need not be initialized before the interface is initialized.

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

* T is a class and an instance of T is created.
* T is a class and a static method declared by T is invoked.
* A static field declared by T is assigned.
* A static field declared by T is used and the reference to the field is not a compile-time constant (�15.28). References to compile-time constants must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field never cause initialization.


It looks like your jvm is just following the rules. This would not be a good exam question because the result is implementation dependent, but it would be a great interview question for the applicant who thinks he knows everything.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks mike,
was eagerly waiting for ur reply
 
reply
    Bookmark Topic Watch Topic
  • New Topic