• 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

initialization

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A{
int length = 10;
double area = length * width; //1
double area = length * this.width; //2
int width

acc to author 2 is correct while 1 is not . can someone pls explain why cause how will compiler knw that there is a field named width

also i was hoping if someone could tell give me some link to a tutorial on initialization order and stuff

also suppose i declare a final variable and decide to declare it in constructor. Does the declaration has to be my first statement in constructor . suppose my first statement is super() or this() , then can I initialize final variable after call to super() or this()
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steven,

Which author are you referring to? And where did you get this example from?

The answer is correct! when you use 'this' referance as in line //2, then the variable is visible but not in line //1 as it throws a illegal forward reference exception.

Can any one throw more light on this exception?

-Praveen

[ February 09, 2006: Message edited by: PraveenPonna ]
[ February 09, 2006: Message edited by: PraveenPonna ]
 
steven gerrard
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got this example frm khalid mughal s book on java certification

i have one more doubt::

static {
sf1 = 10; //1

int a = 2 * sf1; //2

}

static int sf1 = sf2 = 30;

how can above piece of code work . how can 1 work when during static initilizer block in this case is executed first and compiler does not knw wat is sf1 . and if 1 works why doesent 2 work.

i m confused to say the least
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI, steven gerrard .

Static block checks whether the varible is static are not . All the statics are availble to RAM first when the class is loaded. so compiler
knows it.But not with the object,It gives comile time error.
 
steven gerrard
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi thnks for ur explanation

but kindly explain when class is loaded static initializer is loaded first . when the static initializer is loaded compiler does not knw wat sf1 is so it shd give compiler error , but it does not.

now even if it does not give compiler error in the second line when sf1 is used to declare another variable why does it give error cause sf1 has already been initialized in line 1
 
Chandrasekhar Mangipudi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi steven gerrard.
You are Posting Good Questions , So Keep Posting.

I checked in my machine,If i Write like this, It doesnot give any compile error: static {
sf1=20;
int a = 2 * St.sf2;// classname.Static Varibale
} // like " this.width " .
static int sf1=30;
static int sf2=40;

Statics are available to the Ram Directly and Complier checks whether static intializer is available to the Ram or not. If yes , Then Checks any Syntax Errors.

Sekhar SCJP 1.4
[ February 10, 2006: Message edited by: Chandrasekhar Mangipudi ]
 
steven gerrard
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okk thnks fr ur reply
i still have 1 query though . why shd sf1 be referred as St.sf1 and not sf1
 
Chandrasekhar Mangipudi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Compiler Understands that the variable is a static(St.sf1) , and its declaration has been written some where in the code.
[ February 11, 2006: Message edited by: Chandrasekhar Mangipudi ]
 
Your mind is under my control .... your will is now mine .... read this tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic