• 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

Static and Non-Static Blocks

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading about static blocks that are basically statements enclosed in brackets outside of any method or constructor and I think I understand it. However, non-static blocks I am not quite I understand.
With static blocks, would I be right in saying that when the JVM decides to load a class - regardless if a class is instantiated, the static block of code is called.
With non-static blocks, every time a class is instantiated these statements are run.
Could someone give me an idea whether I am on the right track here?
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You are right in your assumptions.
static blocks gets executed when the class gets loaded and
non static blocks are executed when an onject of that class is
created. In other terms we can call that as a parameterless
constructor. For every object created these non static blocks
gets eecuted. we use constructors because it can be overloaded.
In non static block you can also use "this" and "super" where as
in static blocks you cannot use them.
Hope this helps.


------------------
Regards,
V. Kishan Kumar
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Terry,
I think you have it right. The code below proves the point . Run it with parameter 1 for creating the superclass and 2 for creating the subclass.

[I added UBB CODE tags to your source code to make it more readable. Please try to use
them in the future. Learn more about UBB codes here - Ajith ]

[This message has been edited by Ajith Kallambella (edited October 05, 2000).]
 
Harry Singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kishan,
We cannot use this() and super() calls any where except for constructors. Not even in instance initializer blocks.
 
Kishan Kumar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Harry,
If I am not wrong "this" and "super" can be used in Instance
Initializers. Please run this code

will give the output 10.
Hope this helps.
------------------
Regards,
V. Kishan Kumar
[I added UBB CODE tags to your source code to make it more readable. Please try to use
them in the future. Learn more about UBB codes here - Ajith ]

[This message has been edited by Ajith Kallambella (edited October 05, 2000).]
 
Terry McKee
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all of the reponses. I can understand why you would use static blocks, but wouldn't it be good programming to use the actual constructors for coding rather than non-static blocks?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Terry,
Instance initialization block (non-static block !!) is useful for the anonymous inner class because it cannot "define" a constructor.
Following code (though not very useful) shows itz use.
class A{
public static void main(String args[]) {
A a = new A();
B b = a.tester(10);
b.bTester();
B b2 = a.tester2(20);
b2.bTester();

}
public B tester(final int x) {
return new B() {
// Instance variable for the anonymous class.
int i;
int y;
// Use the instance initialization like a constructor
{
i = x;
y = i + x;
}
public void bTester() {
i = i++; // ???
System.out.println("I ---> " + i);
System.out.println("Y ---> " + y);
}
};

}
// 2nd version, this method creates and returns an anonymous inner class which
// extends the abstract class B. The anonymous class calls the 2nd constructor
// of its super class "B", B(int i).
public B tester2(final int x) {
// This anonymous class is not defining a constructor. Its just calling
// the super class constructor
return new B(x) {
// Instance variables for the anonymous class.
int i;
int y;
// Use the instance initialization like a constructor
{
i = x;
y = i + x;
}
public void bTester() {
i = i++; // ???
System.out.println("I (v2) ---> " + i);
System.out.println("Y (v2) ---> " + y);
}
};

}
}
abstract class B {
public B() {
}
public B(int i) {
}
public abstract void bTester();
}

Originally posted by Terry McKee:
Thanks for all of the reponses. I can understand why you would use static blocks, but wouldn't it be good programming to use the actual constructors for coding rather than non-static blocks?


 
Jon Aryan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh... my code indentation lookZ horrible!!!
 
Harry Singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,
You can use this and super for referencing superclass variables and variables of this class any where in the class. But using this() and super() ( not this.i and super.j ) cannot be used in constructors. Lets all keep it in mind because there are so many questions asked on that in the mocks.
Terry, Initializers help us a lot in annonymous classes. Remember there are no constructors in annonymous classes.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Harry,
Have to disagree with you on the use of this and super in constructors; they are allowed.
Please see the following code:

Gives the following output:

The constructor <code>TestCtorB()</code> calls the constructor <code>TestCtorB(String str)</code> using <code>this</code>. In fact, such techniques are recommended in building constructors.
The only prohibition is that you cannot use both <code>super()</code> and <code>this()</code> in the same constructor.

------------------
Jane
[This message has been edited by Jane Griscti (edited October 07, 2000).]
 
Harry Singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, that was a typo. I meant this() and super() cannot be used in static initializers and instance initializers and that is the point Kishan was trying to make.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic