This week's book giveaway is in the Open Source Projects forum.
We're giving away four copies of Eclipse Collections Categorically: Level up your programming game and have Donald Raab on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

static initializer block!

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 {}
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic