• 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

about constructor

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why this program only output 6 on screen?
-------------------------------
class Process {
public static final int x=9;
int y=4;
Process() {
System.out.println(x);
}
Process(int x) {
System.out.println(x);
}
}
public class Test extends Process
{
public static final int x=6;
int y=5;
Test t = new Test();
Test() {
System.out.println(y);
}
public static void main(String[] args) {
System.out.println(x);
}
}
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because in your main method u have only one print statement.
u have to create an instance of the other classes for the other print staements to get executed
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, because the only statement that prints that is executed is
System.out.println(x);
Since x is set to 6, the output is 6. If you're expecting the constructors to execute, you have to instantiate a class,
Alex
 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code compiles fine but when run prints 9 for approximately 50 tomes and then shows EXCEPTION_STACK_OVERFLOW in native code.Why?


------------------
azaman
[This message has been edited by Thomas Paul (edited August 01, 2001).]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because as part of the process of creating a TestPr object you instantiate a TestPr object, which then instantiaites a TestPr object which then instantiaites a TestPr object which then instantiaites a TestPr object which then instantiaites a TestPr object which then instantiaites a TestPr object which then instantiaites a TestPr object which then instantiaites a TestPr object which then instantiaites a TestPr object which then instantiaites a TestPr object which then instantiaites a TestPr object which then instantiaites a TestPr object which then instantiaites a TestPr object.... stack overflow
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops!!!
I kept on reading till the end
Rajani
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic