aspose file tools*
The moose likes Beginning Java and the fly likes About an instance variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About an instance variable" Watch "About an instance variable" New topic
Author

About an instance variable

Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
Hi, I have a very very silly question about an instance variable!
When I create a class and and I create an instance of the class directly inside the class, why does it say that 'stack overflow'?
For example, if I do the following, it fives a stack overflow exception.

public class Customer {
Customer custinClass = new Customer();
public static void main(String args[])
{
Customer cust = new Customer();
}
}

Can anyone please help me?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Jean Fore:
Hi, I have a very very silly question about an instance variable!
When I create a class and and I create an instance of the class directly inside the class, why does it say that 'stack overflow'?
For example, if I do the following, it fives a stack overflow exception.

public class Customer {
Customer custinClass = new Customer();
public static void main(String args[])
{
Customer cust = new Customer();
}
}

Can anyone please help me?



Each time a new instance of the class is created, another instance of the class is created. This is causing recursion. Eventually the stack overflows.
[ February 23, 2006: Message edited by: Keith Lynn ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

When you create an object of a class ("new Customer()"), you call the constructor of the class. This also causes the JVM to initialize all its instance variables. So in your code here's what happens:

1. You create a customer ("new Customer()").
2. Its instance variables are initialized. One of them is a Customer, so...
3. Its instance variables are initialized. One of them is a Customer, so...
4. Its instance variables are initialized. One of them is a Customer, so...
5. Its instance variables are initialized. One of them is a Customer, so...
... and so on ad infinitum.

Fortunately it's pretty likely that your code is wrong. Your Customer has a Customer? That doesn't sound right. However from the code you posted it's impossible to tell what is right instead.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Hi Jean,

Welcome to JavaRanch!

Here's what your program does: main() calls new Customer(). Java creates an empty new Customer object, then starts initializing its member variables. The member "custinClass" is initialized with a new Customer object, so Java creates that object, and then starts initializing its member variables. The member "custinClass" is initialized with a new Customer object, so Java creates that object, and then starts initializing its member variables. The member "custinClass" is initialized with a new Customer object, so Java creates that object, and then starts initializing its member variables. The member "custinClass" is initialized with a new Customer object, so Java creates that object, and then starts initializing its member variables. The member "custinClass" is initialized with a new Customer object, so Java creates that object, and then starts initializing its member variables....

And eventually, all stack memory is filled up with partially constructed Customer objects, and Java remembering how to get back to main().

There's nothing wrong with Customer having a reference to another Customer, but that second Customer has to be passed in from outside the class as a constructor argument, and it has to be possible to construct a Customer whose custinClass member is null (with an alternate constructor, for example, or by passing in null to the first constructor.)


[Jess in Action][AskingGoodQuestions]
Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
Thank you so much for the response. But I would like to make some basics clear here since I am slightly confused about some basic concepts.

I understand that main() method is the very entry point to the program and it's a static method. So you cannot use a non-static global reference variable to create an object in that method, and you need to create an object in the main() method using a local reference variable. And you can use this reference variable to call any other non-static method of this particular class.
Then in that case, is there any advantage of having a static reference variable to create an object of a class just as we discussed in the first question? I mean
public class Customer {
static Customer custinClass = new Customer();
make any sense? Do we use an object like this usually?

Thanks again.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Let's back up one step.

What is the instance and/or static variable for? Why does it need to be there at all?
Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
Hi Ernest,
Wow Cool!!! I learned about instance variables and static variables and tried few programs. Now I understand my question itself did not make any sense.
Thank you for the suggestion Ernest !

Jean
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About an instance variable