• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Why interface not need prior intialization??????

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/* Why prior intialization is not necessary in Interfaces*/
/* If i run following program it runs but if interface I1 is not intialize
At loading of class so how does T2 get intialized in class
pls hlp me out*/
interface I1{
public static int T1=56;
public static int T2=(int)(Math.random()*20);
}

interface I2 extends I1{
int T3=89;
}
public class a implements I2{
public static void main(String args[]){
System.out.println(T1);
System.out.println(T2);
System.out.println(T3);
}
}
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yogesh,
All variables declared in interfaces are public static final. This is true if you declare them so or not.
In your interface I1, you are initializing T2 with some random integer. When the class is loaded all static variables are initialized first. That is the time when T2 gets initialized. Once defined its value cannot be changed.
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic