Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About an instance variable

 
Jean Fore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Jean Fore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic