• 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

initialisations and constructor

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Q34
{
static int a;
int b;
public Q34()
{
int c;
c=a;
a++;
b+=c;
}
public static void main(String args[])
{
new Q34();
}
}
will the above code fail since the const is trying to
1)access static members
2)use st var a before it is initialised.
3)use mem var b before its init.
4)use local var c before init.
5)or it will compile
please throw some light
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi dengri,
The above given code will compile properly,reason being as follow's
1) static varaible a is initialized at class load time with a default value of 0 and instance varaible b will get initlized to default value of zero when the constructor of class is created.
2)now,Please come inside the constructor, here c is declared as local varaible without any intializing value but u have assigned the value of a to c before using the c varaible(c=a)now, c has the assigned value of a which is 0, therfore c now contain's 0, meaning initlized to some value.After that u are incrementing the value of a(a++)which is ok.
3)In Next step,(b+=c),u are using varaible b and c in an expression(b+=c)By now ,Both the global variable as well as local varaible are properly initlized as a result no compile time error.
Remeber the main thing is that before using the local varaible(a varaible declared inside the method)it has to be initialized explixitly whereas global varaible's are initialized with default value if not explicitly specified.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i had run the code it compiles and runs fine. My doubt is can a local variable declared inside constructor.
thanks
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a local varaible is a varaible which can be declared inside the any method.Constructor are also method's.u cannot declare a static varaible inside a method beause it belong's to the class a whole.
 
reply
    Bookmark Topic Watch Topic
  • New Topic