Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Help???

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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);
}
}

 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic