This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Please explain the output of this !!!!  RSS feed

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

//Title: Java Learning
//Version:
//Copyright: Copyright (c) 1999
//Author: Saumil
//Company: Softbite
//Description:
package trial;
class statictest1
{
static {
System.out.println("Static");
}
public statictest1()
{
System.out.println("Constructor");
}
public static void main(String[] args)
{
System.out.println("in main[]");
}
public static void main (String args)
{
System.out.println("Not static");
}
}
public class statictest extends statictest1
{
public static void main(String[] args)
{
statictest t= new statictest();
String[] ar = {"Hello"};
System.out.println("in new main[]");
main(ar);
}
}
Thanks in advance.

------------------
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure exactly what you mean by explain the output: do you mean "explain why I get a never ending stream of lines printed out" or do you mean "I understand that I have a recursive call to statictest.main() in statictest.main() and have provided no way of escaping it, please explain why I get the specific lines I get". If it is the first one then the answer is the recursive call. You call main from itself and there is no boundary condition to test, so the program can't escape. The stack keeps on building until it overflows and you get an stack overflow error.
If you mean why do you get this output:
Static
followed by this output repeatedly:
Constructor
in new main[]
Constructor
in new main[]
Constructor
in new main[]
Constructor
in new main[]
Constructor
...
these points might help:
-the repetition is due to the recursive call.
-the code
in statictest1 does not define a method with the proper signature to be called as main for the file. ie. args is a String and not an array of Strings. This is just an overload not an override. But that doesn't really matter because...
-when you invoke java with the command line java statictest the base class's main is not invoked because it is called statictest1.main(). In this case main() is overridden. This explains the "in new main[]" lines.
-
here you create on instance of a sub-class of a base class whos constructor prints out the line "Constructor". Everytime a class with a base class is instantiated (which is to say: everytime a class other than Object is instantiated) the constructors of all of its base classes are called all the way up the object heirarchy to Object. This explains the "Constructor" lines.
-
This quote from Bruce Eckels "Thinking in Java" may shed some light.
<blockquote>It appears to be a method, but it�s just the static keyword followed by a method body. This code, like other static initializations, is executed only once, the first time you make an object of that class or the first time you access a static member of that class (even if you never make an object of that class). </blockquote>
also you have to remember that statictest has the same type as statictest1 (that's part of what it means to extend a class). This should explain why you get one line of "Static" (and only one line) before all the other lines.
Sorry for being so long winded, but it was hard to know exactly what you wanted to know. I hope i've managed somewhere in all that to answer your question. Cheers!
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void main(String[] args){} is just a method like any others. So you can explicitly call main(new String[10]) what not.
 
saumil shukla
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rodney.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!