• 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

Object Creation

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The following program compiles fine. when I run the program I expected "hai" will be printed on the console before "hello".
But "hai" is not printed on console.. why?


public class Test{
Test tst=new Test();
Test(){
System.out.println("hai");
}
public static void main(String args[]){
System.out.println("hello");
}
}

After I changed the above code like the following...

public class Test{
Test tst=new Test();
Test(){
System.out.println("hai");
}
public static void main(String args[]){
Test t=new Test();
System.out.println("hello");
}
}

At Runtime I encountered the follwing Exception.
java.lang.StockOverflowError. Is it a sub class of java.lang.Error.

Any help greatly appriciated.....
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, your second version of the code says the followings:

1.The main method creates an instance of the class 'Test'. (As you altered).
2. Each instance of the Test class will have an instance of Test inside. (HAS-A reference).

It troubles into continuous loop of Test class object instantiation and hence the compiler spits the stack overflow exception.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test
{
static Test oo=new Test();
Test()
{
System.out.println("hai");
}
public static void main(String args[])
{
Test t=new Test();
System.out.println("hello");
}
}

You can try with this ....
[ November 28, 2005: Message edited by: Purujit Saha ]
 
Venkat T
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Ranchers,

Mani vanna has cleared my second doubt.

let's discus about the first version of the program.

Outside the main(), I am creating the object for the Test class.
The compiler won't spit any error,that mean's it's leagal.
so object is created then

why it's not calling the constructor?.

only the hello is printed on console.

cheers.
 
Venkat T
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,


Anybody please explaing why this happens.

public class Test{
Test tst=new Test();
Test(){
System.out.println("hai");
}
public static void main(String args[]){
System.out.println("hello");
}
}

when u say new Test() the constructor gets invoked. It should print "hai"
why it's not happen in this case.

Thank you Mani & sahu

cheers...
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first piece of code doesnt create any objects of the class hence the constructor is not called. This way only 'hello' would be printed. If you modified it a little bit using instead of now when the class is initialized, execution of any class variable initializers and static initializers of the class is also done. This will result in 'hai' printed first followed by 'hello'.
 
Purujit Saha
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Venkat T

Hi Ranchers,


Anybody please explaing why this happens.

public class Test{
Test tst=new Test();
Test(){
System.out.println("hai");
}
public static void main(String args[]){
System.out.println("hello");
}
}

when u say new Test() the constructor gets invoked. It should print "hai"
why it's not happen in this case.



When a class is loaded it first initialise the static variable the executes static block.
then the execution starts from main().

In your code Test tst=new Test(); is a instance variable not a static or class variable.So, its not been initialised.Once you create an object of this class in the main() method then only all inatance variable will be initialise. But in this case its not possible as already explained by Mani vannan.

thnx


-----------------------
So you can do,
public class Test
{
static Test oo=new Test();
Test()
{
System.out.println("hai");
}
public static void main(String args[])
{
System.out.println("hello");
}
}

This is what you are intending to do.
reply
    Bookmark Topic Watch Topic
  • New Topic