• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Constructor doubt

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class cons1 extends F1 {
static String str;
public static void main(String[] args) {
cons1 C1 = new cons1();
C1.str = "S";
cons1 C2 = new cons1();
C2.str = "S20";
System.out.print(C1.str);
}
}
class F {
F(){
System.out.print("F");
}
}
class F1 extends F {
F1(){
System.out.print("F1");
}
}
when im compile this program it shows FF1FF1s20(why its pointing S20)bcoz here we uses to print the System.out.print(C1.str); please explain me
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The secret is in the "static". Be sure that you understand what this means to a variable. The answer is there.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it`s really it`s the magic of static... if you define any member variable static then only copy of the member is created and shared with all the instance of class....

This is the case with member variable..

But case with the static method... behavior is different..

So first understand the magic of the static key word..
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it`s really it`s the magic of static... if you define any member variable static then only copy of the member is created and shared with all the instance of class....

This is the case with member variable..

But case with the static method... behavior is different..

So first understand the magic of the static key word..


kaushik.vira@gmail.com
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic