Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initilization of method

 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A: initializing i to getInt() where k hasnt been initialized yet at this point, its zero. however, how is "1" being returned and printed?
B: prints ret value of method which is 1 and the value of k member var which is 20
 
Rick Salsa
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
Well for A, it is returning 1 because at the time you call the function, k has not been set yet, which would mean 0 + 1.
Explaining the what happens when the class is created might help:
The complier will allocate memory on the heap for all instance variables, and class and method data.
The instance variables are initialized to their default values. In this case 0.
The constructor is invoked. It first calls the constructor of the superclass(es)..
Just before the constructor is executed, all the instance variable initializers are called, then the constructor is executed.
So this is how you are getting 1 back, because k is given a value of 0 before int i = getInt() is called.
Hope that helps explain things a bit.
/rick
[ February 25, 2002: Message edited by: rick salsa ]
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your input Rick
I'm still unsure where this 1 is coming from
0 + 1
Yes I can see that K is 0, but how are we getting 1 for i ?
TIA
 
chafule razgul
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i is assigned to the returned value of getInt()
which returns k+1. At the time i is initialized k is still 0 (yet to be assigned a value, only has the default), hence your i is set to k(which is 0)+1, that is, i is set to 1.
hope this helps
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chafule,

i is assigned to the returned value of getInt()
which returns k+1

I dont see any addition taking place here. What I do see is concatenation k + i where k is zero at that time. yes the return of the method is i but again where is this 1 at?
 
Rick Salsa
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
No concatenation is taking place. The complier knows that k is an int variable. So it adds 0 + 1 and returns it to the caller of the method (int i = ). For the concatenation to happen, the statement would have to be written as:
return "k" + 1;
You will get an error if you try this because the return value of the method is int. If your change i to be a String and change the return value of getInt() to a String from an int, the class with compile and you will k120 printed to the screen.
/rick
[ February 25, 2002: Message edited by: rick salsa ]
 
chafule razgul
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I may be wrong as i'm looking at this on a PC with a bugged VGA card.. but here is the code i cut and pasted from the first post

looks like at line A is where that +1 is
Hope this helps
[Code edited by Valentin]
[ February 25, 2002: Message edited by: Valentin Crettaz ]
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
woops a typo, heres the code that should be:
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually there are more typos (semicolons are missing)...
Moreover, when I run your code the output is
020 which is perfectly correct...
You should also read JLS 8.3.2.3 Restrictions on the use of Fields during Initialization
[ February 25, 2002: Message edited by: Valentin Crettaz ]
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
word
Thank you Velentin
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
run it in my computer, the output is 120, which as my expectation.
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why cann't the code compile?
class TestClass
{
int i = 9;
i = getInt();
int k = 20;
public int getInt()
{
return k + 1; // < -- A
}
public static void main(String[] args)
{
TestClass t = new TestClass();
System.out.println(t.i + "" + t.k); // <-- B
}
}
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you added a statment in a location where it's not allowed:
i = getInt();

You can't have statements outside of methods. You must put this code inside of a method.
If you used code tags it would make it clearer :

[ February 25, 2002: Message edited by: Rob Ross ]
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should have expressions in either a method or a initializer block:
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes,i got it,thank you all.
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone explain how this is different from my code?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your example, you use a declaration with an intializer. The assignement is part of the initialization. The difference is that if you have an assignment on its own without the declaration, that becomes a statement, and statements are not allowed outside of methods or constructors.
int i = 5; //declaration with initilizer valid outside of a method.
i = 5; //assignement statement, only valid inside a method or constructor.
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob!
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,
Don't forget the static/instance initializer blocks too.
Brian
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic