• 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

Class instantiation within the same class

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, this may or may not have been answered here but I'm not sure how to search for this on google. I've tried different variations but nothing seems to return the correct search results.

I am trying to figure why some example code shows a class and within that class shows an object being created with a class type of that class (make sense?). Here is an example:



The portion that I am concerned with is this:

QuizCardBuilder builder = new QuizCardBuilder();

Now, there are plenty of examples that I have seen that do not create an instance of the class within the class. To be honest, I would think that you would run into a never ending loop of objects being created over and over again.

So, can anyone explain to me when this is appropriate and why it is appropriate? Thanks.

Tom

[ December 16, 2008: Message edited by: Tom ]
[ December 16, 2008: Message edited by: Tom ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now, there are plenty of examples that I have seen that do not create an instance of the class within the class. To be honest, I would think that you would run into a never ending loop of objects being created over and over again.



Just because the structure is complex, doesn't mean that it is an "endless loop of objects". But yes, it is possible to have endless construction, in which case you will run out of memory -- either out of memory error on the heap, or out of memory error with a stack overflow.


So, can anyone explain to me when this is appropriate and why it is appropriate? Thanks.



Well, complex data structures are generally used by complex programs. It's kinda hard to keep everything in one class when the code base gets really big. Nor is it a good idea to do so.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The portion that I am concerned with is this:

QuizCardBuilder builder = new QuizCardBuilder();

Now, there are plenty of examples that I have seen that do not create an instance of the class within the class. To be honest, I would think that you would run into a never ending loop of objects being created over and over again.



In rereading your question, I am thinking that you mean something else. Are you implying that the instantiation in main() will cause the endless loop?

First, of all, main() is a method (static in this case, but that's not important). In order for the instantiation to happen, someone has to call the method. In this case, it is by the JVM.

The constructor does not call main() -- creating a new instance doesn't call the main() method. So... only one instance is created.

Henry
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I can tell, Java requires a type for every variable that is declared

ie, String someString = new String();

String is the TYPE of variable;

someString is the variable TYPE String;

= sets the variable and it's type to refrence what it's pointing to.
In this case new String();

When you create a class ClassDog, you create it like this;

ClassDog newDog = new ClassDog();

ClassDog is the TYPE; newDog is the reference variable

Then when you create a instance variable with the same name, you don't declare it as the same TYPE but as as different TYPE ie;

String newDog = new String();

String is the Type; newDog is the reference variable.

with your example code

By declaring type QuizCardBuilder the JVM allocates memory for the "blueprint" of the class QuizCardBuilder, then, it assigns the refrence variable builder to refer to this specific class model, then with the new invocation actually builds the object from the "blueprint" .

The reason that you have to build the object in the main is that the JVM needs a way to tell what object to build first in your program. The JVM then accesses the class blueprint and runs the static method main, you can run a static method in a class before it becomes an object from just the blueprint of the object. The JVM runs the static method which then creates an object of the QuizCardBuilder class blueprint.

This gives life to


and under additional code there must be a non-static Go() method in this class because the line after the creation of the QuizCardBuilder class says

builder.go();

So if we added that to your code



While you can access static methods as well as static variables before an Object of the Class from the blueprint in this case the main method,

you cannot access any of the non-static methods until the class blueprint has been made into an Object with the new operator

Hope that makes sense;

Have a good day
[ December 16, 2008: Message edited by: Chadd Franck ]
reply
    Bookmark Topic Watch Topic
  • New Topic