• 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:

need explanation

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

here is the code and output, i want explanation.

class A{
static int i=5;
static{
System.out.println("A");
}
}

class B extends A{
static int i1=6;
static {
System.out.println("B");
}
}

public class Test {
public static void main(String arg[]){
System.out.println(B.i);
}
}

The output is :
A
5

why not
A
B
5
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what the Java Language Specification says about static initializers.

Any static initializers declared in a class are executed when the class is initialized

I believe that the reason the static initializer of B does not execute is that we are directly accessing it's static member, but we aren't initializing the class.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If you change the variable i1 to i, the output will be A B 5. static blocks will be executed when the corresponding class is loaded. In your case, the class B was not loaded because you were accessing the A class' variable through B. If you change i1 to i, you are accessing the class B's varible. Hence class B is loaded and hence the corresponding static initializer block is executed.

I am not sure about this though. Pls correct me if am wrong.

Cheers,
Arvind
[ February 21, 2006: Message edited by: Arvind Sampath ]
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if you change the line to print B.i1 or change i1 to i, the static initializer of B is executed.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

public class Test {
public static void main(String arg[]){
System.out.println(B.i);}
}



I think u can't access i from B. It should give u a compile time error. because static variables are not inherited.
Please let me know if I am wrong.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think u can't access i from B. It should give u a compile time error. because static variables are not inherited.
Please let me know if I am wrong.



static membes are inherited.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static members cannot be inherited, but can be hided.
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I also agree with Harshil concerning static members can be inherited. However, static methods CANNOT be overriden.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

Could anyone explain to me what does the following actually mean/do?

static
{
System.out.println("A");
}

I came across static method,variable but not static written in this way. Can someone explain it to me?

Thanks.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I also agree with Harshil concerning static members can be inherited. However, static methods CANNOT be overriden



But i am able to run the following code

public class Apple {
static int z=6;

public static void disp(){
System.out.println("In Apple"+z);
}


}
class orange extends Apple{

public static void main(String agrs[]){

orange.disp();
}
public static void disp(){
System.out.println("In Orange"+z);
}


}

Is disp method is not overridden ???

plz clarify........
 
Murali Mohan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No disp method is not overidden. The disp method in orange class hides the disp method of Apple class.
[ February 22, 2006: Message edited by: Murali Mohan ]
 
Murali Mohan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Could anyone explain to me what does the following actually mean/do?

static
{
System.out.println("A");
}

I came across static method,variable but not static written in this way. Can someone explain it to me?



This is called static initializer block. This code will be executed when the classs containing this code is first loaded into the VM.
To knoe more about this you can refer any good book on java.
 
Your mother is a hamster and your father smells of tiny ads!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic