wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes initilization of method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "initilization of method" Watch "initilization of method" New topic
Author

initilization of method

Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172

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

Joined: Jul 17, 2001
Posts: 173
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

Joined: Jan 17, 2002
Posts: 172
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

Joined: Feb 09, 2002
Posts: 63
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

Joined: Jan 17, 2002
Posts: 172
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

Joined: Jul 17, 2001
Posts: 173
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

Joined: Feb 09, 2002
Posts: 63
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

Joined: Jan 17, 2002
Posts: 172
woops a typo, heres the code that should be:
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
word
Thank you Velentin
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
run it in my computer, the output is 120, which as my expectation.


help you means help me
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
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

Joined: Jan 07, 2002
Posts: 2205
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 ]

Rob
SCJP 1.4
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
You should have expressions in either a method or a initializer block:
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
yes,i got it,thank you all.
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
Could someone explain how this is different from my code?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Jan 17, 2002
Posts: 172
Thanks Rob!
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
Rob,
Don't forget the static/instance initializer blocks too.
Brian
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: initilization of method