Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

static initializer block!  RSS feed

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi! why does the given code not compile?? one more thing pl expalain , what do we mean by time of class loading regarding static variables and static initializers?
thanx.
ashok.
________________
public class fort {
static inter ob = new inter() {
static { System.out.println("hi"); }
};
}
interface inter {}
 
Gold Digger
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler says
Test.java:5: Method static {} can't be static in local class Test. 1. Only members of interfaces and top-level classes can be static.
static { System.out.println("hi"); }
^
which means that since you are defining an anonymous inner class here, it is neither an interface nor a top-level class.
Val
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting...
My compiler (jdk 1.3 on win 95 ) says...


C:\Temp>javac fort.java
fort.java:3: inner classes cannot have static declarations
static { System.out.println("hi"); }
^
1 error


Shyam
 
Valentin Crettaz
Gold Digger
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah sorry I forgot to mention that I was running Java 2 SDK 1.4 beta 2 on Linux...
But this is quite clear that an anonymous cannot be static and thus need not have static initializers. It may have instance initializers though !
Val
[This message has been edited by Valentin Crettaz (edited September 24, 2001).]
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Even object of interface can't be initiated.Only refernec of interface can be declared.
Regards,
Hassan.
 
Valentin Crettaz
Gold Digger
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to disagree with you Hassen, look at the following code

we have a reference to a Runnable and it has been declared and instantiated, works fine, where's the problem with instantiating an interface ? Interface can't be instantiated, that's true, but with anonymous inner classes you can get that behavior...
Anyone correct me if I'm wrong !
Val
 
Get out of my mind! Look! A tiny ad!
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!