• 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!