• 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:

Help???

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why it goes unnoticed
Any body help me with this, plz!Hi,all;
got a burning question here, as a matter of fact, this question raised before, but I did not get it.
//code follows
Please take a look at the code below:
Please take a look at the code below:
public class Test
{
private int i = getJ();
private int j = 10;
private int getJ()
{
return j;
}
public static void main(String[] args)
{
System.out.println(new Test().i);
}
}

will print 0.
//cited from previous posting.
My question is why does compiler not flag an error in the first place since j has not yet been declared when i is to be initialized ?
Can anybody can explain to me what is going on here? Exactly what is sequence of execution during initialization?
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Order of Initialization of variables depends on the order that the variables are defined within the class.Even if the variable definitions are scattered through out method definitions the variables are initialized before any methods can be called....even the constructor.
This example has been taken from Thinking in Java.Relate this example to your code,you will get it.
class Tag {
Tag(int marker) {
System.out.println("Tag(" + marker + ")");
}
}
class Card {
Tag t1 = new Tag(1);
Card() {
System.out.println("Card()");
t3 = new Tag(33);
}
Tag t2 = new Tag(2);
void f() {
System.out.println("f()");
}
Tag t3 = new Tag(3);
}
public class OrderOfInitialization {
public static void main(String[] args) {
Card t = new Card();
t.f();
}
}
will print out :
Tag(1)
Tag(2)
Tag(3)
Card()
Tag(33)
f()
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jason:

1) My question is why does compiler not flag an error in the first place since j has not yet been declared when i is to be initialized ?
2) Can anybody can explain to me what is going on here? Exactly what is sequence of execution during initialization?


Jason,
The compiler does not complains for method declared after or before the line it is used. But it complains if it�s related to variables.
1) The variable i is not inicialized but it was declared. The compiler just check for declaration statments. in fact, it is cunfusing but we should note that it is a very poor OOP style. The Compiler help us to write good code but if we do not want ...
2) During inicialization, the sequence is what we would expect for that code. To be clearly, try to change declaring and initialized lines, like this:
private int i = getJ();
private int j = 10; //Here, i = 0
private int j = 10;
private int i = getJ(); //Here, i = 10
So the initialized sequence would not be the broblem.
IMO we should not use something like this. It�s not Object Oriented Programming.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I agree with Surya B. From your program just change the order and you will output of 10.
public class Test{
private int j = 10;
private int i = getJ();
private int getJ(){
return j;
}
public static void main(String[] args){
System.out.println(new Test().i);
}
}

 
I will suppress my every urge. But not this shameless plug:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic