• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

pls help exams are on my head

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test {
static int a=5;
static int b=a;

static
{
this.a = ++a;
}
public static void main(String[] args) {
System.out.println( a + b );
}
}
Prints 11. True or False?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This will gives u compilation error stating that not static variable "this" can not be accessed from static Context.
~Kumar
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static vars mean it is belong to the class.
Static vars get initialized when the class is loaded the first time and "this." is a object reference to the object itself.
So, you can use this. on static method or static {}.

sorry, typo... i mean u can't use this.
on static method or static {}.
[This message has been edited by FEI NG (edited November 08, 2001).]
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
False
The compiler will complain: the non-static variable this cannot be referenced from a static context
ie
this.a = ++a;
^
The answer would be true (return 11)if a in the static initializer were referenced as:
Test.a = ++a;
A static initializer has all access to static data and methods of its class
Hope this helps
rgds Jim
[This message has been edited by Jim Petersen (edited November 08, 2001).]
 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh forgot something

Good luck on your exam!!!
Good luck on your exam!!!
Good luck on your exam!!!
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the order of initialization
static var or static initializer ??? I thought the static initializer come first.
 
Jim Petersen
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pervez
Order of initialization
1. static variables initialization.
2. static initializer block execution. (in the order of declaration, if multiple blocks found)
3. constructor header ( super or this � implicit or explicit )
4. instance variables initialization / instance initializer block(s) execution
5. rest of the code in the constructor
I would have thought a variable would have to initialize before a block so it could be used still I could stand corrected if that is not entirely accurate
Hope this helps - Jim
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think first all the static variables and initializer block well get initialised in the order of the execution.
I hope this code may help it :-

Since we are using the c before initialising it , it will give compilation error--Can't make forward reference to c.
Regard's,
Sumit
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Sumit,
U R right. it will initialize the order in which the static variables and static blocks are present in the program.
~Kumar.
 
Jim Petersen
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I agree static variables and initializer blocks seem to be processed in order of appearance!
ie

will not compile due to illegal forward references - Thks for clearing up this doubt
rgds - Jim
[This message has been edited by Jim Petersen (edited November 09, 2001).]
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic