• 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

member assignmnet

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how come this will not produce any error ?
class Test {
Test() { k = 2; }
int j = 1;
int i = j;
int k;
}
can anybody explain ?
rahul
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Rahul by the title of your post I assume you are refering to the assignment in the constructor Test() {k = 2;} There is nothing wrong with this statement. In fact that is often what a constructor is used for is to initialize member variables. Now if you had a constructor such as:
Test() {
int k = 4; // local (automatic) variable
this.k = 2; //referering to the member variable k
}
In this case because you've defined a local variable with the same name you'd have to use this.k to access the member variable k. But elsewise the way you have your constructor is fine!
Hope this answers your question!
Christopher
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test {
Test() { k = 2; }
int j = 1;
int i = j;
int k;
}
Hi,
First variables initiallization take place and after that excecution of constructor.
1. j=1, i=1 and k=0(default)
2. After constructor execution k=2
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Now I know that the order of execution is NOT Dependant on the order it is written in the class file. So you will have the in k default to 0 BEFORE it is set to 2 in the constructor.
When you create an object instance it will to the following. (I'll have to have this exact order confirmed though).
1- Initialize and set (default if necessary) any member variables.
2- Processes any "static" blocks.
3- Calls any super classes if nescessary (restarting at step 1 for the super class before continuing.)
3- executes the current constructor.
So the k=2 in the constructor is perfectly legal, and, it IS NOT overwritten by the default initialization of "k" 'after' the construtor.
k is defaulted to 0 way before the constructor is executed.
Hope this helps.
And I hope I didn't repeat myself too much.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static block executes before the instance initializer .

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic