• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

methods or what?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 public class Q24
2 {
3 final static int fsn;
4 final int fn;
5 static
6 {
7 fsn=6;
8 }
9 {
10 fn =8;
11 }
12 }

1 public class initialize
2 {
3 public initialize(int m)
4 {
5 System.out.println(i+","+j+","+k+","+x+","+m);
6 }
7 public static void main(String[]args)
8 {
9 initialize obj=new initialize(x);
10 }
11 static int i=5;
12 static int x;
13 int j=7;
14 int k;
15 {
16 j=70;x=20;
17 }
18 static
19 {
20 i=50;
21 }
22 }

In these two programs there are braces, in the first code, on line 5, and in the second code, on line 15. What is this? There is no name of a method or return type or anything. Is this actually possible, and what does it do? Or are they mistakes made by the author?
Thanking You,
Kamil
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kamil,
These code blocks with only {} r called initializer code blocks or initializers. There r static (line 18) or non-static (line 15)initialzers in ur code. Anyway u r decalring two public classes in a single source file which is a compile time error...

------------------
azaman
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
These are not mistakes by Author.These are Initializer Block,where in you can initialize your instance variables.And the code on lone 5 in the first snippet is a static initializer block.
See JLS for more details.
Thanx
 
Kamil Dada
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
Can someone please explain the concept of static blocks/instance initializers to me?
How do they work, what do they do, whats the use of them, how would I use them, and when would I want/need to use them?
and/or provide some good links explaining these as well?
Thank You,
Kamil.
[This message has been edited by Kamil Dada (edited August 14, 2001).]
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Kamil,
You can get information about blocks at: http://java.sun.com/docs/books/jls/first_edition/html/14.doc.html#24644
Regards
Gurpreet Sachdeva
For mock exams and other useful pages related to JCP plz. visit at: http://www.go4java.20m.com
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kamil,
Initialization blocks are useful if you need an involved initialization statement for fields. For example, you may have a static field to hold the names of the months. You can declare a <code>static String[] months = new String[11];</code> and then place all the actual initialization code in a static initializer, just to make things a little neater.
Static initialization blocks are run only once, when the class is loaded. Instance initializer blocks are executed once for each new instance.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Kamil Dada
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jane Griscti:
Hi Kamil,
Initialization blocks are useful if you need an involved initialization statement for fields. For example, you may have a static field to hold the names of the months. You can declare a <code>static String[] months = new String[11];</code> and then place all the actual initialization code in a static initializer, just to make things a little neater.
Static initialization blocks are run only once, when the class is loaded. Instance initializer blocks are executed once for each new instance.
Hope that helps.


Hello,
Hmm so if i did:

and regarding static initializers since they are only run once when the class is loaded (loaded; meaning what?)
if i did:

Am I right?
Thank You,
Kamil.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kamil,
See JLS §12.2 for information on how the JVM loads a class or interface. Basically, 'load' means to store the class information in memory.
<code>Someclass a = new SomeClass()</code> doesn't call the initialization block; the process of instantiating the class executes any initialization block.
If it's a <code>static</code> initializer block, it only gets executed once, when the JVM initially loads it into memory.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Kamil Dada
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Jane, thanks for your help.
I really appreciate it.
-Kamil
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for this wonderful discussion, Can somebody tell me the order by which initializer, variables, methods, constructor are initialized when class is loaded or instantiated.
What is diff b/w class loading and instantiating.
AAA
--Farooq
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic