• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

initilization of method

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
woops a typo, heres the code that should be:
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
word
Thank you Velentin
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
}
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes,i got it,thank you all.
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob!
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,
Don't forget the static/instance initializer blocks too.
Brian
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic