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

Core Concept needed

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The following code gives the compilation error

public class AQuestion
{
private int i = j;
private int j = 10;



public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}



This code executes corrrectly and gives the output 0

public class AQuestion
{
private int i = giveMeJ();
private int j = 10;

private int giveMeJ()
{
return j;
}

public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}

Why is it so bcos in both the cases "j" is declared after i.

thanks in advance
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first code : you have to declare file j before field i;
The second code : var j in method not the same field j in class. You can break point in System.out.println((new AQuestion()).i).
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijaya,


Why is it so bcos in both the cases "j" is declared after i.



The order of declaration is not making any difference in the code you mentioned. Whenever a class in instantiated, all the instance variables are given thier default values. However, before the initialization is done, you can not assign any existing variable or can not refer any existing reference.

in the second code you posted,
is not assigning variable 'j' to 'i' but it's assigning value '0' returned by method 'giveMeJ()' to 'i'. So it's legal.

hth.
 
vijaya vinayagam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Harshil Mehta ,
as you said in the 2nd code the method returns "j" but when we see the flow of control in the first line it calls the method and at that time "j" is not even declared

pls. explain i'am new
 
Harshil Mehta
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijaya,


as you said in the 2nd code the method returns "j" but when we see the flow of control in the first line it calls the method and at that time "j" is not even declared.



"i" and "j" both are declared and are assigned their default values as soon as object of the class is loaded in the heap. but this initialization does not allow us to refer them in assignment or any arithmatic operation.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Harshil Mehta:
The order of declaration is not making any difference in the code you mentioned. Whenever a class in instantiated, all the instance variables are given thier default values. However, before the initialization is done, you can not assign any existing variable or can not refer any existing reference.



Maybe I misunderstood you, but when we switch these two lines:

private int i = j;
private int j = 10;

to:

private int j = 10;
private int i = j;

the following code compiles and the output is 10.

public class AQuestion
{
private int j = 10;
private int i = j;;

public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
 
catch it before it slithers away! Oh wait, it's a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic