• 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

forward reference question

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Initializer expressions and instance initialized blocks are executed in the order they are specified in the class. But why

is legal, and

is illegal?

[ December 16, 2005: Message edited by: Dmitryi Neverov ]
[ December 16, 2005: Message edited by: Dmitryi Neverov ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Dmitryi,

Please confor that the code like this


class Comm{
public void ss(){
k=10;
int k=11;
}
int k;
}



IF yes then it is legal.


class Comm{
public void ss(){
k=10;
int k=11;
}
///int k;
}



if k is not declared as classe member then it is illega.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variable declared in the { } is only valid within the block
So you can declare another j outside
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think there is one primitive only. Because in the other case, the variable j inside the static block doesn't have the type(so, we can't assume it to be int). while the declaration is done outside the static block, initialization is done inside static block. It has got to do with the order of executing of static block and the instance variables. But, if that is the case a static block gets executed(at class loading) before the instance variable initialization is done. So, it should have errored out for case.1....confused!!!
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class d{j = 3;int j = 5;}

j=3; is a expression, you cannot define an expression in a class.it can only be inside a method.

where as in the previous class
we have the instance initilizer(expressions are valid), and there its initializing the variable.it is fine
 
You’ll find me in my office. I’ll probably be drinking. And reading 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