File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Creation

 
Venkat T
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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.....
 
Mani vannan
Ranch Hand
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Purujit Saha
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
rey anz
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic