• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

What will be the output?Why? - extension

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers!

This question is somewhat similar to the one posted in topic - "What will be the output?Why?"


class Base {
static int value = 0;
Base() {
addValue();
}
static void addValue() {
value +=10;
}
int getValue() {
return value;
}
}

class Derived extends Base {
Derived() {
addValue();
}
static void addValue() {
value +=20;
}
}

public class Test {
public static void main(String args[]) {
Base b= new Derived();
System.out.println(b.getValue());
}
}

ANS: 30

Pls explain why the ouput is 30?

Thanks
Neha
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Base b= new Derived();


child class constructor by default calls constructor of base class.
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey

Here all are static methods.All will be executed when the jvm load the class.
initially the value of Value is 0.
It loads the Derived class ,so the addValue() method will be executed,the value of Value is 10,and then loads the Base class now the value is 30.

Static variable has only one copy.All the instances of that class share that value.

That's why you are getting output as 30

Thanks

Anil Kumar
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you called child constructor first statement is called super()

so the value =10 and then will called addValue static method in the child

so the value = 30 ,
 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neha,

Overriding is only for non static methods in your previous post where addValue() method was overriden in sub class but here both are static so it depends on the reference from which you are calling this method.
if the reference is of type Base then base version of static method will get called.

Regards,

Abdul Mohsin
 
Neha Monga
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay..i got it somewhat..to make it more clear..what if the declaration of variable 'value'in Base class is changed to -> int value = 0;

will this change the output?

the methods addValue() remain static in both classes.

pls explain..
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Then you will get compile time error.
Because you cannot access a nonstatic variable from a static context.Because there is no this in static contex.


Thanks
Anil Kumar
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
neha, basically you are confused between the static and non static declarations.
static means, at class level, only one reference. they will be initialized at the time of loading the class that means at the time of compilation. so, they can not see non static elements.
in this case, both methods are static. echa class haves their own reference of the method.
when super class constructor calls, it calls the super class's(of it own) reference of the addvalue method.
so, the value will be 10.
when base call constructor call the addvalue method, it calls its own method.
so the value is 30(10+20).

if you make value non static, the static blocks can not see them. then they will shout where is the value variable is??
hope you got the point.
 
Neha Monga
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup Krishna..Got it...

Thanks All
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic