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

Initialization

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In the below program, I am expecting compilation error according to JCP book as m is not declared before using it. But the program works fine and gives output. Can anybody explain how this program works.

public class Init
{
private static String msg(String msg)
{
System.out.println(msg);
System.out.println("Open for all");
return msg;
}


public Init()
{
m=msg("1");
System.out.println("I am near 1");
}

{
m=msg("2");
System.out.println("I am near 2");
}

String m=msg("3");
{
System.out.println("I am near 3");
}

public static void main(String args[])
{
Object obj=new Init();
System.out.println("I am in main1");
}
}
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First i would like to say Hi to all java ranch.
I compile and run this code ya it's compile and run fine with output.
you know m is already declared within constructor that why code is compile & fine run with output

Here is code



Maybe this will help
 
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sarathy

Before you understand the working of the above question, it is important to understand the working and order of execution of the following:

1) Instance initializer block of a class
2) Instance variable initializers.
3) Constructor of a class.


Instance initializer block
===================

An instance initializer is a set of statements that appear within a pair of curly brackets in an class. The following are instance initializers in your class Init:

Instance initializer 1
---------------------

{
m=msg("1");
System.out.println("I am near 1");
}

Instance initializer 2
---------------------

{
System.out.println("I am near 3");
}



Instance variable initializers.
====================

Following is a Instance variable initializer in your class:

String m=msg("3");



Order of execution of the above mentioned points
==================================

1. The instance variables are created and accessible to all the instance methods and instance initializers of a class, even before they are initialized.

2. The instance initializers and variable initializers execute in the order that they appear in the class.

3. The constructor of a class executes after the execution of the instance initializers.


All the instance initializers of a class execute after the execution of the super class constructors.





I hope this explains why you were able to use the instance variable 'm' even before its declaration and why the numbers 2 and 3 were printed before the number 1.


cheers

Mala
 
The only thing that kept the leeches off of me was this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic