aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object Creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object Creation" Watch "Object Creation" New topic
Author

Object Creation

Venkat T
Greenhorn

Joined: Nov 28, 2005
Posts: 3
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.....
Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
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.


Manivannan
Purujit Saha
Ranch Hand

Joined: Nov 01, 2005
Posts: 86
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

Joined: Nov 28, 2005
Posts: 3
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

Joined: Nov 28, 2005
Posts: 3
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...
rey anz
Greenhorn

Joined: Nov 10, 2005
Posts: 7
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

Joined: Nov 01, 2005
Posts: 86

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Creation
 
Similar Threads
object refference
doubt in scanner
Can Varible be used to create a Method ?
Constructor not executing? Help !!
cofused about output of program..?